logger

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2023 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// HTTP response codes groups regex defined in the RFC 9110.
	HTTPInformationalRegex = "^1[0-9]{2}$"
	HTTPSuccessfulRegex    = "^2[0-9]{2}$"
	HTTPRedirectionRegex   = "^3[0-9]{2}$"
	HTTPClientErrorRegex   = "^4[0-9]{2}$"
	HTTPServerErrorRegex   = "^5[0-9]{2}$"
)

Variables

This section is empty.

Functions

func New

func New(logger *slog.Logger, opts ...ConfigOption) gin.HandlerFunc

New returns a gin.HandlerFunc (middleware) that logs requests using slog.

By default, the log level depend on the HTTP return code:

  • 1XX return codes are logged at INFO level.
  • 2XX return codes are logged at INFO level.
  • 3XX return codes are logged at INFO level.
  • 4XX return codes are logged at WARN level.
  • 5XX return codes are logged at ERROR level.

By default, all paths are logged. Whitelist and Blacklist can be used to filter the paths.

By default, the following fields are logged:

  • IP address
  • Status code
  • HTTP method
  • Path
  • User agent
  • Latency
  • Request ID (X-Request-ID header)

Types

type Config

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

Config represents the logging middleware configuration.

type ConfigOption

type ConfigOption func(*Config)

ConfigOption allows to customize the middleware config.

func WithBlacklistPath

func WithBlacklistPath(blacklistPath []string) ConfigOption

WithBlacklistPath allows to blacklist paths. It panics if the regex is invalid. If a blacklist is set, all paths except blacklisted are logged.

func WithCustomFields

func WithCustomFields(customFields CustomFields) ConfigOption

WithCustomFields allows to set a custom function to add custom fields to the log line.

func WithCustomFilter

func WithCustomFilter(customFilter CustomFilter) ConfigOption

WithCustomFilter allows to set a custom filter function.

func WithCustomLogger

func WithCustomLogger(customLogger CustomLogger) ConfigOption

WithCustomLogger allows to set a custom logger function.

func WithDefaultLevel

func WithDefaultLevel(level slog.Level) ConfigOption

WithDefaultLevel allows to set the default log level.

func WithHTTPLevels

func WithHTTPLevels(httpLevels map[string]slog.Level) ConfigOption

WithHTTPLevels allows to set the log level based on the HTTP return code. The map key is a regex to match the HTTP return code. It panics if the regex is invalid.

func WithWhitelistPath

func WithWhitelistPath(whitelistPath []string) ConfigOption

WithWhitelistPath allows to whitelist paths. It panics if the regex is invalid. If a whitelist is set, only whitelisted paths are logged.

func WithoutDefaultFields

func WithoutDefaultFields() ConfigOption

WithoutDefaultFields to not use the default fields in the log line.

func WithoutIP

func WithoutIP() ConfigOption

WithoutIP to not add the IP address to the log line.

func WithoutLatency

func WithoutLatency() ConfigOption

WithoutLatency to not add the request latency to the log line.

func WithoutMethod

func WithoutMethod() ConfigOption

WithoutMethod to not add the HTTP method to the log line.

func WithoutPath

func WithoutPath() ConfigOption

WithoutPath to not add the HTTP path to the log line.

func WithoutRequestID

func WithoutRequestID() ConfigOption

WithoutRequestID to not add the X-Request-ID header to the log line.

func WithoutStatus

func WithoutStatus() ConfigOption

WithoutStatus to not add the HTTP return code to the log line.

func WithoutUserAgent

func WithoutUserAgent() ConfigOption

WithoutUserAgent to not add the user agent to the log line.

type CustomFields

type CustomFields func(c *gin.Context) []slog.Attr

CustomFields allows to add custom fields to the log line.

type CustomFilter

type CustomFilter func(c *gin.Context) bool

CustomFilter allows to filter the log line. Return true to log the line, false otherwise.

type CustomLogger

type CustomLogger func(c *gin.Context, logger *slog.Logger)

CustomLogger allows to call a custom logger function.

Jump to

Keyboard shortcuts

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