httplog

package module
v1.0.1-0...-b6e21ca Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2023 License: MIT Imports: 13 Imported by: 0

README

httplog

Small but powerful structured logging package for HTTP request logging in Go.

Did a quick and dirty modification of this package. Now it uses a passed in configured Logger, instead of returning a new one, and taking it in as a value. I'm mostly using Zerolog anyways in my projects, and I'm going to configure it elsewhere, and want the logging middleware to adapt. Now it does

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = Options{
	LogLevel:        "info",
	LevelFieldName:  "level",
	JSON:            true,
	Concise:         true,
	Tags:            nil,
	SkipHeaders:     nil,
	TimeFieldFormat: time.RFC3339Nano,
	TimeFieldName:   "timestamp",
}

Functions

func Configure

func Configure(opts Options)

Configure will set new global/default options for the httplog and behaviour of underlying zerolog pkg and its global logger.

func Handler

func Handler(logger *zerolog.Logger) func(next http.Handler) http.Handler

func LogEntry

func LogEntry(ctx context.Context) zerolog.Logger

func LogEntrySetField

func LogEntrySetField(ctx context.Context, key, value string)

func LogEntrySetFields

func LogEntrySetFields(ctx context.Context, fields map[string]interface{})

func RequestLogger

func RequestLogger(logger *zerolog.Logger) func(next http.Handler) http.Handler

RequestLogger is an http middleware to log http requests and responses.

NOTE: for simplicity, RequestLogger automatically makes use of the chi RequestID and Recoverer middleware.

Types

type Options

type Options struct {
	// LogLevel defines the minimum level of severity that app should log.
	//
	// Must be one of: ["trace", "debug", "info", "warn", "error", "critical"]
	LogLevel string

	// LevelFieldName sets the field name for the log level or severity.
	// Some providers parse and search for different field names.
	LevelFieldName string

	// JSON enables structured logging output in json. Make sure to enable this
	// in production mode so log aggregators can receive data in parsable format.
	//
	// In local development mode, its appropriate to set this value to false to
	// receive pretty output and stacktraces to stdout.
	JSON bool

	// Concise mode includes fewer log details during the request flow. For example
	// excluding details like request content length, user-agent and other details.
	// This is useful if during development your console is too noisy.
	Concise bool

	// Tags are additional fields included at the root level of all logs.
	// These can be useful for example the commit hash of a build, or an environment
	// name like prod/stg/dev
	Tags map[string]string

	// SkipHeaders are additional headers which are redacted from the logs
	SkipHeaders []string

	// TimeFieldFormat defines the time format of the Time field, defaulting to "time.RFC3339Nano" see options at:
	// https://pkg.go.dev/time#pkg-constants
	TimeFieldFormat string

	// TimeFieldName sets the field name for the time field.
	// Some providers parse and search for different field names.
	TimeFieldName string
}

type RequestLoggerEntry

type RequestLoggerEntry struct {
	Logger zerolog.Logger
	// contains filtered or unexported fields
}

func (*RequestLoggerEntry) Panic

func (l *RequestLoggerEntry) Panic(v interface{}, stack []byte)

func (*RequestLoggerEntry) Write

func (l *RequestLoggerEntry) Write(status, bytes int, header http.Header, elapsed time.Duration, extra interface{})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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