Giter VIP home page Giter VIP logo

go-mongolog's Introduction

Mongolog

Simple Logrus :walrus: 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

go-mongolog's People

Contributors

geronimo794 avatar

Watchers

 avatar  avatar  avatar

Forkers

cybersshell

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.