ctxlog

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2018 License: Apache-2.0 Imports: 4 Imported by: 1

README

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)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func From

func From(ctx context.Context) log.Logger

From is a helper function to extract a Logger from a context. If no ctxlog.Logger exists in the context, a NopLogger is returned.

Types

type HTTPMiddleware

type HTTPMiddleware struct {
	// contains filtered or unexported fields
}

HTTPMiddleware uses the Logger to implement basic structured request logging.

func NewHTTPMiddleware

func NewHTTPMiddleware(next http.Handler, logger log.Logger) *HTTPMiddleware

NewHTTPMiddleware wraps an http.Handler and a log.Logger, and performs structured request logging.

func (*HTTPMiddleware) ServeHTTP

func (mw *HTTPMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger satisfies log.Logger and is designed to be constructed into a context via New. Components can retrieve it from the context via From, and use the Log method to append keyvals. The entrypoint (e.g. an http.Handler) should Flush at the end of its lifecycle (e.g. the end of the request).

func New

func New(ctx context.Context, initialKeyvals ...interface{}) (context.Context, *Logger)

New is a helper function to create a new Logger, log the initial set of keyvals, inject it into a context, and return everything, all in one motion.

func (*Logger) Keyvals

func (logger *Logger) Keyvals() []interface{}

Keyvals returns the keyvals that have been collected by the Logger, and can be passed to the Log method of a concrete logger.

func (*Logger) Log

func (logger *Logger) Log(keyvals ...interface{}) error

Log implements go-kit/kit/log.Logger, buffering keyvals into memory. Use Keyvals at the end of the lifecycle to log to a concrete logger. Log always succeeds.

Jump to

Keyboard shortcuts

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