mongolog

package module
v0.0.0-...-e23d53c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2023 License: MIT Imports: 7 Imported by: 0

README

Mongolog

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

  1. 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")
    }
    
  2. 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")
    
    }	
    
  3. 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")
    }	
    
  4. 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")
    
    }	
    
  5. 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

  1. 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)
    }
    
  2. 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)
    }
    
  3. 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)
    }
    
  4. 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)

Planned Update

  1. Failover File Logging: Create failover mechanisme when failed to write on mongodb database. [DONE]

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHook

func NewHook(h string, p string, u string, pass string, db string, coll string) (*hook, error)

func NewHookClient

func NewHookClient(client *mongo.Client, db string, coll string) (*hook, error)

func NewHookCollection

func NewHookCollection(collection *mongo.Collection) (*hook, error)

func NewHookConnectionString

func NewHookConnectionString(cs string, db string, coll string) (*hook, error)

func NewHookDatabase

func NewHookDatabase(database *mongo.Database, coll string) (*hook, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL