Simple Logrus hook for MongoDB.
Main feature: Async Feature, Write Timeout, Context Setting, and Failover File Logging
$ go get -u github.com/geronimo794/go-mongolog
- 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") }
- 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)
log := logrus.New() hook, err := mongolog.NewHook(mongoHost, mongoPort, mongoUsername, mongoPassword, db, coll) if err == nil { hook.SetIsAsync(true) log.Hooks.Add(hook) } else { fmt.Print(err) }
- 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.
log := logrus.New() hook, err := mongolog.NewHook(mongoHost, mongoPort, mongoUsername, mongoPassword, db, coll) if err == nil { hook.SetWriteTimeout(1 * time.Second) log.Hooks.Add(hook) } else { fmt.Print(err) }
- Context Setting(default=context.Background()): You can set custom context to pass it to the hook. You can set it via SetContext method.
log := logrus.New() hook, err := mongolog.NewHook(mongoHost, mongoPort, mongoUsername, mongoPassword, db, coll) if err == nil { // Create context with timeout ctx, ctxCancelFunc := context.WithTimeout(context.TODO(), 1*time.Second) hook.SetContext(ctx) log.Hooks.Add(hook) defer ctxCancelFunc() } else { fmt.Print(err) }
- 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.
log := logrus.New() hook, err := mongolog.NewHook(mongoHost, mongoPort, mongoUsername, mongoPassword, db, coll) if err == nil { err = hook.SetFailoverFilePath("mongolog-failover.log") if err == nil { log.Hooks.Add(hook) } else { fmt.Print(err) } } else { fmt.Print(err) }
*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)
- Failover File Logging: Create failover mechanisme when failed to write on mongodb database. [DONE]
MIT