Categorygithub.com/mia-platform/glogger/v2
modulepackage
2.1.3
Repository: https://github.com/mia-platform/glogger.git
Documentation: pkg.go.dev

# README

Glogger

Build Status Go Report Card GoDoc

Glogger is the logger for mia-platform go services.

It uses logrus as logging library, and implements a middleware to be used with http gorilla mux router.

This library follow the Mia Platform logging guidelines.

Install

This library require golang at version >= 1.13

go get -u github.com/mia-platform/glogger/v2

Example usage

Basic logger initialization.

The allowed log level are those parsed by logrus ParseLevel (e.g. panic, fatal, error, warn, warning, info, debug, trace).

// Logger setup
log, err := glogger.InitHelper(glogger.InitOptions{Level: "info"})
if err != nil {
	msg := fmt.Sprintf("An error occurred while creating the logger: %v", err)
	panic(msg)
}

Setup log middleware

Init log middleware for mux router. This log the incoming request and request completed following the mia-platform guidelines.

r := mux.NewRouter()
r.Use(glogger.RequestMiddlewareLogger(log, nil))

and, to retrieve logger injected in request context:

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

with excluded path

You can restrict the path where the logger middleware take effect using the second paramenter in RequestMiddlewareLogger. For example, this could be useful to exclude incoming request and request completed logging in path router.

Logger function is injected anyway in request context.

r := mux.NewRouter()
r.Use(glogger.RequestMiddlewareLogger(log, []string{"/-/"}))

How to log error message

To log error message using default field

_, err := myFn()

if err != nil {
  glogger.Get(req.Context()).WithError(err).Error("error calling function")
}

How to log custom fields

To log error message using default field

glogger.Get(req.Context()).WithField("key", "some field").Info("error calling function")

glogger.Get(req.Context()).WithFields(&logrus.Fields{
  "key": "some field",
  "another-key": "something"
}).Info("log with custom fields")

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

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

# Functions

Get retrieves the current logger from the context.
InitHelper is a function to init json logger.
RequestMiddlewareLogger is a gorilla/mux middleware to log all requests with logrus It logs the incoming request and when request is completed, adding latency of the request.
WithLogger returns a new context with the provided logger.

# Structs

Host has the host information.
HTTP is the struct of the log formatter.
InitOptions is the struct of options to configure the logger.
JSONFormatter struct formats logs in JSON following Mia-Platform guidelines.
Request contains the items of request info log.
Response contains the items of response info log.
URL info.