logger

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StructuredLogger

func StructuredLogger(opts ...Option) middleware.MiddlewareFunc

StructuredLogger creates middleware that logs request and response information using structured logging.

By default, it uses slog.Default() as the logger. A custom logger can be specified using the WithLogger option.

The middleware logs:

  • Before handler execution: Request information (excluding Body, but including Body size)
  • After handler execution: Response information (excluding Body, but including Body size), error if any, and execution duration

Example:

// Use with default logger
handler := middleware.Use(myHandler, logger.StructuredLogger())

// Use with custom logger
customLogger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
handler := middleware.Use(myHandler, logger.StructuredLogger(WithLogger(customLogger)))

Types

type Config

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

Config represents the configuration for the StructuredLogger middleware.

type Option

type Option func(*Config)

Option is a function type to modify the StructuredLogger configuration.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets a custom logger for the StructuredLogger middleware.

func WithRequestBodyLogging

func WithRequestBodyLogging(enable bool) Option

WithRequestBodyLogging enables or disables request body logging in the middleware. By default, logging is disabled.

func WithResponseBodyLogging

func WithResponseBodyLogging(enable bool) Option

WithResponseBodyLogging enables or disables response body logging in the middleware. By default, logging is disabled.

Jump to

Keyboard shortcuts

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