logger

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = &Config{
	Output: os.Stderr,
	Format: TextFormat,
	Flags:  StdLogFlags,
	Options: &slog.HandlerOptions{
		Level:     slog.LevelInfo,
		AddSource: false,
	},
}

DefaultConfig is the default logger used by the Logger middleware. It writes logs to os.Stderr with the TextFormat and StdLogFlags. The log level is set to Info.

Functions

func New

func New(config *Config) rex.Middleware

New returns a new Logger middleware with the provided configuration. The logger needs access to status code and thus must apear before middleware wrapping the default response writer (like etags and Brotli)

Types

type Config

type Config struct {
	// Output is the destination for the log output. If nil, os.Stderr is used.
	Output io.Writer

	// Format is the format of the log output. Default is TextFormat.
	Format LogFormat

	// Flags is the flags to be used for logging. Default is StdLogFlags.
	Flags LogFlags

	// Skip is a slice of paths that should not be logged.
	Skip []string

	// SkipIf is a function that can be used to skip logging based on the request.
	// If it returns true, the request will not be logged.
	SkipIf func(r *http.Request) bool

	// Options is the options to be passed to the slog.Handler.
	Options *slog.HandlerOptions

	// Callback is a function that can be used to modify the arguments passed to the logger.
	// Forexample the request_id, user_id etc.
	Callback func(r *http.Request, args ...any) []any
}

Config is a middleware that logs the request and response information.

func (*Config) Logger

func (l *Config) Logger(next rex.HandlerFunc) rex.HandlerFunc

Logger is the middleware handler function for LoggerMiddleware.

type LogFlags

type LogFlags int8
const (
	LOG_IP LogFlags = 1 << iota
	LOG_LATENCY
	LOG_USERAGENT
)
const StdLogFlags LogFlags = LOG_LATENCY | LOG_IP

type LogFormat

type LogFormat int

LogFormat is the format of the log output, compatible with the new slog package.

const (
	TextFormat LogFormat = iota + 1 // This is the default format
	JSONFormat                      // Log in JSON format
)

Jump to

Keyboard shortcuts

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