goji / glogrus2

Logrus logging for Context-based Goji

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

goji/glogrus GoDoc

glogrus2 provides structured logging via logrus for Goji2.

Example

Simple logging

package main

import(
	"github.com/goji/glogrus2"
    "goji.io"
    "net/http"
    "github.com/Sirupsen/logrus"
)

func main() {
    router := goji.NewMux()
	logr := logrus.New()
	logr.Formatter = new(logrus.JSONFormatter)
	goji.Use(glogrus.NewGlogrus(logr, "my-app-name"))

	log.Fatal(http.ListenAndServe(":8080", router))
}

Logging with custom request id from http Context

package main

import(
	"github.com/goji/glogrus2"
    "goji.io"
    "golang.org/x/net/context"
    "net/http"
    "github.com/Sirupsen/logrus"
)

func main() {
    router := goji.NewMux()
	logr := logrus.New()
	logr.Formatter = new(logrus.JSONFormatter)
	router.UseC(glogrus.NewGlogrusWithReqId(logr, "my-app-name", IdFromContext))

	log.Fatal(http.ListenAndServe(":8080", router))
}

func IdFromContext(ctx context.Context) string {
    return ctx.Value("requestIdKey")
}

Need something to put requestId in your Context?

gojiid can help you with that

Looking for hierarchical structured logging?

slog and lunk looks interesting.

About

Logrus logging for Context-based Goji

License:MIT License


Languages

Language:Go 100.0%