Documentation
¶
Index ¶
- func Get(c *gin.Context) *slog.Logger
- func ParseLevel(levelStr string) (slog.Level, error)
- func SetLogger(opts ...Option) gin.HandlerFunc
- type EventFn
- type Fn
- type Option
- func WithClientErrorLevel(lvl slog.Level) Option
- func WithContext(fn func(*gin.Context, *slog.Record) *slog.Record) Option
- func WithDefaultLevel(lvl slog.Level) Option
- func WithHiddenRequestHeaders(headers []string) Option
- func WithLogger(fn func(*gin.Context, *slog.Logger) *slog.Logger) Option
- func WithMessage(message string) Option
- func WithPathLevel(m map[string]slog.Level) Option
- func WithRequestHeader(enabled bool) Option
- func WithServerErrorLevel(lvl slog.Level) Option
- func WithSkipPath(s []string) Option
- func WithSkipPathRegexps(regs ...*regexp.Regexp) Option
- func WithSkipper(s Skipper) Option
- func WithSpecificLogLevelByStatusCode(statusCodes map[int]slog.Level) Option
- func WithUTC(s bool) Option
- func WithWriter(s io.Writer) Option
- type Skipper
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Get ¶
Get retrieves the *slog.Logger instance from the given gin.Context. It assumes that the logger has been previously set in the context with the key loggerKey. If the logger is not found, it will panic.
Parameters:
c - the gin.Context from which to retrieve the logger.
Returns:
*slog.Logger - the logger instance stored in the context.
func ParseLevel ¶
ParseLevel parses a string representation of a log level and returns the corresponding slog.Level. It takes a single argument:
- levelStr: a string representing the log level (e.g., "debug", "info", "warn", "error").
It returns:
- slog.Level: the parsed log level.
- error: an error if the log level string is invalid.
func SetLogger ¶
func SetLogger(opts ...Option) gin.HandlerFunc
SetLogger returns a gin.HandlerFunc (middleware) that logs requests using slog. It accepts a variadic number of Option functions to customize the logger's behavior.
The logger configuration includes:
- defaultLevel: the default logging level (default: slog.LevelInfo).
- clientErrorLevel: the logging level for client errors (default: slog.LevelWarn).
- serverErrorLevel: the logging level for server errors (default: slog.LevelError).
- output: the output writer for the logger (default: gin.DefaultWriter).
- skipPath: a list of paths to skip logging.
- skipPathRegexps: a list of regular expressions to skip logging for matching paths.
- logger: a custom logger function to use instead of the default logger.
The middleware logs the following request details:
- method: the HTTP method of the request.
- path: the URL path of the request.
- ip: the client's IP address.
- user_agent: the User-Agent header of the request.
- status: the HTTP status code of the response.
- latency: the time taken to process the request.
- body_size: the size of the response body.
The logging level for each request is determined based on the response status code:
- clientErrorLevel for 4xx status codes.
- serverErrorLevel for 5xx status codes.
- defaultLevel for other status codes.
- Custom levels can be set for specific paths using the pathLevels configuration.
Types ¶
type EventFn ¶
EventFn is a function type that takes a gin.Context and a *slog.Record as parameters, and returns a *slog.Record. It is typically used to modify or enhance the record within the context of a Gin HTTP request.
type Fn ¶
Fn is a function type that takes a gin.Context and a *slog.Logger as parameters, and returns a *slog.Logger. It is typically used to modify or enhance the logger within the context of a Gin HTTP request.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
func WithClientErrorLevel ¶
WithClientErrorLevel sets client error log level (400-499).
func WithContext ¶
WithContext sets a custom context handler for slog.Record.
func WithDefaultLevel ¶
WithDefaultLevel sets config defaultLevel (<400 status).
func WithHiddenRequestHeaders ¶ added in v1.1.0
WithHiddenRequestHeaders sets request header names to be hidden. Only works with WithRequestHeader enabled.
func WithLogger ¶
WithLogger sets a logger function to the config.
func WithMessage ¶
WithMessage sets a custom log message for requests.
func WithPathLevel ¶
WithPathLevel sets path-specific logging levels.
func WithRequestHeader ¶ added in v1.1.0
WithRequestHeader enables/disables logging all HTTP request headers.
func WithServerErrorLevel ¶
WithServerErrorLevel sets server error log level.
func WithSkipPath ¶
WithSkipPath sets URL path list for skipPath in config.
func WithSkipPathRegexps ¶
WithSkipPathRegexps appends regexp rules for skipPathRegexps in config.
func WithSkipper ¶
WithSkipper sets a function to skip logging for certain requests.
func WithSpecificLogLevelByStatusCode ¶
WithSpecificLogLevelByStatusCode sets specific log level per HTTP status.