Documentation
¶
Index ¶
- Constants
- func New(logger *slog.Logger, opts ...ConfigOption) gin.HandlerFunc
- type Config
- type ConfigOption
- func WithBlacklistPath(blacklistPath []string) ConfigOption
- func WithCustomFields(customFields CustomFields) ConfigOption
- func WithCustomFilter(customFilter CustomFilter) ConfigOption
- func WithCustomLogger(customLogger CustomLogger) ConfigOption
- func WithDefaultLevel(level slog.Level) ConfigOption
- func WithHTTPLevels(httpLevels map[string]slog.Level) ConfigOption
- func WithWhitelistPath(whitelistPath []string) ConfigOption
- func WithoutDefaultFields() ConfigOption
- func WithoutIP() ConfigOption
- func WithoutLatency() ConfigOption
- func WithoutMethod() ConfigOption
- func WithoutPath() ConfigOption
- func WithoutRequestID() ConfigOption
- func WithoutStatus() ConfigOption
- func WithoutUserAgent() ConfigOption
- type CustomFields
- type CustomFilter
- type CustomLogger
Constants ¶
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 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 ¶
CustomFields allows to add custom fields to the log line.
type CustomFilter ¶
CustomFilter allows to filter the log line. Return true to log the line, false otherwise.