glogrus

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

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

Go to latest
Published: Nov 28, 2019 License: MIT Imports: 6 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewGlogrus

func NewGlogrus(l *logrus.Logger, name string) func(goji.Handler) goji.Handler

NewGlogrus allows you to configure a goji middleware that logs all requests and responses using the structured logger logrus. It takes the logrus instance and the name of the app as the parameters and returns a middleware of type "func(goji.Handler) goji.Handler"

Example:

package main

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

func main() {

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

	goji.Get("/ping", yourHandler)
	goji.Serve()
}

func NewGlogrusWithReqId

func NewGlogrusWithReqId(l *logrus.Logger, name string, reqidf func(context.Context) string) func(goji.Handler) goji.Handler

NewGlogrusWithReqId allows you to configure a goji middleware that logs all requests and responses using the structured logger logrus. It takes the logrus instance, the name of the app and a function that can retrieve a requestId from the Context "func(context.Context) string" as the parameters and returns a middleware of type "func(goji.Handler) goji.Handler"

Passing in the function that returns a requestId allows you to "plug in" other middleware that may set the request id

Example:

package main

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

func main() {

	logr := logrus.New()
	logr.Formatter = new(logrus.JSONFormatter)
	goji.Use(glogrus.NewGlogrusWithReqId(logr, "my-app-name", GetRequestId))

	goji.Get("/ping", yourHandler)
	goji.Serve()
}

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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