Skip to content

riccardotzr/glogger

Repository files navigation

Glogger

Build Status Go Report Card Go Reference

Glogger is a go logging library. It uses logrus library and implements a middleware to be used with Gorilla Mux.

Install

go get -u github.com/riccardotzr/glogger

Usage

Logger initialization

log, err := glogger.Init(glogger.InitOptions{Level: "info"})

if err != nil {
    panic(err.Error())
}

Middleware initialization

r := mux.NewRouter()
r.Use(glogger.LoggingMiddleware(log))

and to retrieve logger injected in request context:

func (w http.ResponseWriter, r *http.Request) {
    logger := glogger.Get(r.Context())
    logger.Info("My log message")
}

Logging Error Message

To log error message using default field

func (w http.ResponseWriter, r *http.Request) {
    logger := glogger.Get(r.Context())
    
    _, err := fn()

    if err != nil {
        logger.WithError(err).Error("My error message")
    }
}

Logging Custom Fields

To log error message using default field

func (w http.ResponseWriter, r *http.Request) {
    logger := glogger.Get(r.Context())
    logger.WithFields(&logrus.Fields{
        "key": "my_key",
    }).Info("Log with custom fields")
}

License

This project is licensed under the Apache License 2.0 - see the LICENSE.md file for details

About

Go logging library based on logrus library

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages