Skip to content
This repository has been archived by the owner on Sep 27, 2021. It is now read-only.

peterbourgon/ctxlog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ctxlog

Create wide log events in Go programs.

When requests first hit your system, use the New constructor to create a request logger and inject it into a context in one motion. During request processing, use the From constructor to extract the logger from the context and add keyvals. At the end of request processing, use Keyvals to get all of the added data and report it somewhere.

import (
	"github.com/go-kit/kit/log"
	"github.com/peterbourgon/ctxlog"
)

func handleRequest(ctx context.Context, ..., logger log.Logger) {
	subctx, ctxlogger := ctxlog.New(ctx)
	process(subctx)
	logger.Log(ctxlogger.Keyvals()...)
}

Package ctxlog comes with a default HTTP middleware that does this setup and teardown work for you, and adds a few useful default keyvals.

var h http.Handler
{
	h = myHandler
	h = ctxlog.NewHTTPMiddleware(h, logger)
}

About

Create wide log events in Go programs

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages