loggingmiddleware

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: BSD-3-Clause Imports: 12 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustNew

func MustNew(c Config) endpoint.Middleware

MustNew calls New and panics in case of error.

func New

func New(c Config) (endpoint.Middleware, error)

New returns a new go-kit logging middleware with the given name and configuration.

Fields Config.Name and Config.Logger are mandatory. Considering that this middleware puts a logger inside the context, this should always be the outter middleware when using endpoint.Chain.

func WithRequestMeta

func WithRequestMeta(ctx context.Context, val Meta) context.Context

WithRequestMeta returns a context the given metadata. This value is retrieved by the logging middleware and logged along other information. This is intended to be used by transport layers to send data to the logging middleware on the endpoint layer.

Types

type Config

type Config struct {
	// Name is the name of the endpoint.
	// It must not be empty and will be logged as endpoint_name.
	Name string

	// TruncRequestAt will truncate the serialized request if
	// the string length is above this value.
	// Setting to zero disables truncating.
	TruncRequestAt int

	// TruncResponseAt will truncate the serialized request if
	// the string length is above this value.
	// Setting to zero disables truncating.
	TruncResponseAt int

	// LogRequestIfLevel will log the request if this
	// log level is enabled in the logger.
	LogRequestIfLevel zerolog.Level

	// LogResponseIfLevel will log the response if this
	// log level is enabled in the logger.
	LogResponseIfLevel zerolog.Level

	// SuccessLevel is the log level of requests that are processed without error.
	SuccessLevel zerolog.Level

	// DefaultErrorLevel is the log level of requests that finished with some error.
	// May be override by ErrorCodeMapLevel or SeverityMapLevel
	DefaultErrorLevel zerolog.Level

	// ErrorCodeMapLevel is used to override the error level using the error code.
	// This is optional
	ErrorCodeMapLevel map[errors.Code]zerolog.Level

	// SeverityMapLevel is used to override the error level using the error severity.
	// This is optional
	SeverityMapLevel map[errors.Severity]zerolog.Level

	// Logger is the default logger to be put in the context.
	// If there is already a logger in the context, the context
	// is not updated and the existing logger is used.
	Logger *zerolog.Logger

	// Meta are extra keys logged under 'endpoint_meta' key.
	Meta Meta

	// EnrichRequestFunc is used to enrich the logger context with the request.
	//
	// It is called by the middleware to enrich the logger with information from
	// inside the request just before calling the next endpoint. This function should
	// interpret the request and extract information and return the new zerolog context.
	//
	// Remember that the request can be logged as a whole by using LogRequestIfLevel and
	// TruncRequestAt.
	//
	// If set to 'nil' it is disabled.
	EnrichLogWithRequest EnrichLogWithRequestFunc

	// EnrichLogWithResponse is used to enrich the logger context with the response and error.
	//
	// It is called by the middleware to enrich the logger with information from
	// inside the respose and error after calling the next endpoint. This function should
	// interpret the response and error and extract information and return the new zerolog context.
	//
	// Remember that the response can be logged as a whole by using LogResponseIfLevel and
	// TruncResponseAt.
	//
	// If set to 'nil' it is disabled.
	EnrichLogWithResponse EnrichLogWithResponseFunc
}

Config is a struct that configures the behavior of a loggingmiddleware. All fields should be set unless marked as optional.

func NewDefaultConfig

func NewDefaultConfig(name string) Config

NewDefaultConfig contains sane defaults to configure a new logging middleware.

type EnrichLogWithRequestFunc

type EnrichLogWithRequestFunc func(ctx context.Context, zctx zerolog.Context, request interface{}) (context.Context, zerolog.Context)

EnrichLogWithRequestFunc is a function that receives a zerolog Context and the request. It should parse and extract information and return a new enriched zerolog context.

type EnrichLogWithResponseFunc

type EnrichLogWithResponseFunc func(ctx context.Context, zctx zerolog.Context, response interface{}, err error) zerolog.Context

EnrichLogWithResponseFunc is a function that receives a zerolog Context and the response and error. It should parse and extract information and return a new enriched zerolog context. The error is already handled by the logging middleware if it is an error.Error, but it is passed to this functions should you need to extract another kind of data from the error.

type LoggableEndpointRequest

type LoggableEndpointRequest interface {
	// EnrichLog enriches the current log context @zctx with the request data.
	EnrichLog(
		ctx context.Context,
		zctx zerolog.Context,
	) (context.Context, zerolog.Context)
}

LoggableEndpointRequest, if implemented, by the endpoint request type, will be used to enrich the log along with the EnrichLogWithRequest config.

type LoggableEndpointResponse

type LoggableEndpointResponse interface {
	// EnrichLog enriches the current log context @zctx with the response data.
	EnrichLog(
		ctx context.Context,
		zctx zerolog.Context,
	) zerolog.Context
}

LoggableEndpointResponse, if implemented, by the endpoint response type, will be used to enrich the log along with the EnrichLogWithResponse config.

type Meta

type Meta map[string]string

Meta is a map containing arbitrary metadata to be passed to the logging middleware.

func GetRequestMeta

func GetRequestMeta(ctx context.Context) Meta

GetRequestMeta returns a Meta added in the context by the WithRequestMeta function

Jump to

Keyboard shortcuts

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