Simple Logrus hook for MongoDB.
Main feature: Async Feature, Write Timeout, Context Setting, and Failover File Logging
Download and install
$ go get -u github.com/geronimo794/go-mongolog
Usage
Use with existing connection string (You can use connection string from MongoDB atlas)
package main
import (
"github.com/geronimo794/go-mongolog"
"github.com/sirupsen/logrus"
)
func main() {
// Create mongolog hook: Atlas mongodb service
var connectionString = "mongodb+srv://<username>:<password>@<host>/?retryWrites=true&w=majority"
var db = "db"
var coll = "coll"
hook, err := mongolog.NewHookConnectionString(connectionString, db, coll)
// New logrus instance
log := logrus.New()
if err == nil {
log.Hooks.Add(hook)
} else {
log.Panic(err)
}
// Create log example
log.WithFields(logrus.Fields{
"name": "Ach Rozikin",
}).Error("Error log")
log.Warn("Warning log")
log.Error("Error log")
}
Use with mongoDB configuration host, port, username, password.
package main
import (
"github.com/geronimo794/go-mongolog"
"github.com/sirupsen/logrus"
)
func main() {
// Create mongolog hook
var mongoHost = "localhost"
var mongoUsername = ""
var mongoPassword = ""
var mongoPort = "27017"
var db = "db"
var coll = "coll"
hook, err := mongolog.NewHook(mongoHost, mongoPort, mongoUsername, mongoPassword, db, coll)
// Create logrus instance
log := logrus.New()
if err == nil {
log.Hooks.Add(hook)
} else {
log.Panic(err)
}
// Create log example
log.WithFields(logrus.Fields{
"name": "Ach Rozikin",
}).Error("Error log")
log.Warn("Warning log")
log.Error("Error log")
}
Use with pre existiong *mongo.Client
package main
import (
"context"
"log"
"time"
"github.com/geronimo794/go-mongolog"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
// Create mongoDB client with connection string
// All this option is optional
serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1)
clientOptions := options.Client().
ApplyURI("mongodb+srv://<username>:<password>@<host>/?retryWrites=true&w=majority").
SetServerAPIOptions(serverAPIOptions)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
// Create mongolog with client variable
var db = "db"
var coll = "coll-test"
hook, err := mongolog.NewHookClient(client, db, coll)
// New logrus instance and hook
log := logrus.New()
if err == nil {
log.Hooks.Add(hook)
} else {
log.Panic(err)
}
// Create log example
log.WithFields(logrus.Fields{
"name": "Ach Rozikin",
}).Error("Error log")
log.Warn("Warning log")
log.Error("Error log")
}
Use with pre existiong *mongo.Database
package main
import (
"context"
"log"
"time"
"github.com/geronimo794/go-mongolog"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
// Create mongoDB client with connection string
// All this option is optional
serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1)
clientOptions := options.Client().
ApplyURI("mongodb+srv://<username>:<password>@<host>/?retryWrites=true&w=majority").
SetServerAPIOptions(serverAPIOptions)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
clientDb := client.Database("db")
// Create mongolog with client database variable
var coll = "coll-test"
hook, err := mongolog.NewHookDatabase(clientDb, coll)
// New logrus instance and hook
log := logrus.New()
if err == nil {
log.Hooks.Add(hook)
} else {
log.Panic(err)
}
// Create log example
log.WithFields(logrus.Fields{
"name": "Ach Rozikin",
}).Error("Error log")
log.Warn("Warning log")
log.Error("Error log")
}
Use with pre existiong *mongo.Collection
package main
import (
"context"
"log"
"time"
"github.com/geronimo794/go-mongolog"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
// Create mongoDB client with connection string
// All this option is optional
serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1)
clientOptions := options.Client().
ApplyURI("mongodb+srv://<username>:<password>@<host>/?retryWrites=true&w=majority").
SetServerAPIOptions(serverAPIOptions)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
clientDbCollection := client.Database("db").Collection("coll-test")
// Create mongolog with client database collection variable
hook, err := mongolog.NewHookCollection(clientDbCollection)
// New logrus instance and hook
log := logrus.New()
if err == nil {
log.Hooks.Add(hook)
} else {
log.Panic(err)
}
// Create log example
log.WithFields(logrus.Fields{
"name": "Ach Rozikin",
}).Error("Error log")
log.Warn("Warning log")
log.Error("Error log")
}
Feature
Async Feature(default=false): Create the process of writing to mongodb asyncronushly. To activate the feature, call SetIsAsync with parameter true. If you use this feaature, make sure don't close the application before the process is done (You can use time.Sleep(XXX * time.Second) to prevent application from close it's self)
Write Timeout(default=0): Add write timeout feature to set max timeout when writing data to the mongodb instance. You can set it via SetWriteTimeout method.
Failover File Logging(default=nil): Set the failover file to save the log when the application failed to save the log on mongodb. You can set it via SetFailoverFilePath method.
*You can use SetIsAsync and SetWriteTimeout combination to prevent your application for goroutine leak.
*If you are using SetWriteTimeout and Context Setting at once. Context Setting value will be ignored by the hook process.
*Be aware when using the Failover File Logging, keep this log is clean and clear (It's mean mongodb always success to save the log)
Planned Update
Failover File Logging: Create failover mechanisme when failed to write on mongodb database. [DONE]