logging

package
v0.0.0-...-4fb4148 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LogFormatLogfmt = "logfmt"
	LogFormatJSON   = "json"
)

Variables

View Source
var RequestIDCtxKey struct{}

Functions

func DefaultCodeToLevel

func DefaultCodeToLevel(logger log.Logger, code int) log.Logger

DefaultCodeToLevel is the helper mapper that maps HTTP Response codes to log levels.

func DefaultErrorToCode

func DefaultErrorToCode(_ error) int

DefaultErrorToCode returns an InternalServerError.

func DefaultFilterLogging

func DefaultFilterLogging(logger log.Logger) log.Logger

DefaultFilterLogging allows logs from all levels to be logged in output.

func NewLogger

func NewLogger(logLevel, logFormat, debugName string, w io.Writer) log.Logger

NewLogger returns a log.Logger that prints in the provided format at the provided level with a UTC timestamp and the caller of the log entry. If non empty, the debug name is also appended as a field to all log lines. Panics if the log level is not error, warn, info or debug. Log level is expected to be validated before passed to this function.

Types

type CodeToLevel

type CodeToLevel func(logger log.Logger, code int) log.Logger

CodeToLevel function defines the mapping between HTTP Response codes to log levels for server side.

type Decider

type Decider func(methodName string, err error) Decision

Decider function defines rules for suppressing the logging.

type Decision

type Decision int

Decision defines rules for enabling start and end of logging.

const (
	// NoLogCall - Logging is disabled.
	NoLogCall Decision = iota
	// LogFinishCall - Only finish logs of request is enabled.
	LogFinishCall
	// LogStartAndFinishCall - Logging of start and end of request is enabled.
	LogStartAndFinishCall
)

func DefaultDeciderMethod

func DefaultDeciderMethod(_ string, _ error) Decision

DefaultDeciderMethod is the default implementation of decider to see if you should log the call by default this is set to LogStartAndFinishCall.

type DurationToFields

type DurationToFields func(duration time.Duration) Fields

DurationToFields function defines how to produce duration fields for logging.

type ErrorToCode

type ErrorToCode func(err error) int

ErrorToCode function determines the error code of the error for the http response.

type Fields

type Fields []string

Fields represents logging fields. It has to have even number of elements (pairs).

func DurationToTimeMillisFields

func DurationToTimeMillisFields(duration time.Duration) Fields

DurationToTimeMillisFields converts the duration to milliseconds and uses the key `http.time_ms`.

type FilterLogging

type FilterLogging func(logger log.Logger) log.Logger

FilterLogging makes sure only the logs with level=lvl gets logged, or filtered.

type HTTPMiddleware

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

func NewHTTPServerMiddleware

func NewHTTPServerMiddleware(logger log.Logger, opts ...Option) *HTTPMiddleware

NewHTTPServerMiddleware returns an http middleware.

func (*HTTPMiddleware) WrapHandler

func (m *HTTPMiddleware) WrapHandler(name string, next http.Handler) http.HandlerFunc

type Option

type Option func(*options)

func WithDecider

func WithDecider(f Decider) Option

WithDecider customizes the function for deciding if the HTTP Middlewares/Tripperwares should log.

func WithFilter

func WithFilter(f FilterLogging) Option

WithFilter customizes the function for deciding which level of logging should be allowed. Follows go-kit Allow<level of log> convention.

func WithLevels

func WithLevels(f CodeToLevel) Option

WithLevels customizes the function for mapping HTTP response codes and interceptor log level statements.

type ResponseWriterWithStatus

type ResponseWriterWithStatus struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

ResponseWriterWithStatus wraps around http.ResponseWriter to capture the status code of the response.

func WrapResponseWriterWithStatus

func WrapResponseWriterWithStatus(w http.ResponseWriter) *ResponseWriterWithStatus

WrapResponseWriterWithStatus wraps the http.ResponseWriter for extracting status.

func (*ResponseWriterWithStatus) Status

func (r *ResponseWriterWithStatus) Status() string

Status returns http response status.

func (*ResponseWriterWithStatus) StatusCode

func (r *ResponseWriterWithStatus) StatusCode() int

StatusCode returns http response status code.

func (*ResponseWriterWithStatus) WriteHeader

func (r *ResponseWriterWithStatus) WriteHeader(code int)

WriteHeader writes the header.

Jump to

Keyboard shortcuts

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