Skip to content

Commit f8558bf

Browse files
committed
docs: improve codebase documentation and comments
- Add detailed comments explaining the `Fn` type and its usage in Gin HTTP requests. - Update the `Skipper` comment to clarify its purpose and functionality. - Improve comments for the `config` struct fields to provide clearer descriptions. - Enhance the comment for the `SetLogger` function to include detailed information about its configuration and logging behavior. - Add comprehensive comments for the `ParseLevel` function, detailing its parameters and return values. - Update the `Get` function comment to explain its purpose and usage in retrieving a logger from the Gin context. Signed-off-by: appleboy <[email protected]>
1 parent 5cf8008 commit f8558bf

File tree

1 file changed

+62
-17
lines changed

1 file changed

+62
-17
lines changed

logger.go

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,69 @@ import (
1212
"github.com/rs/zerolog"
1313
)
1414

15+
// Fn is a function type that takes a gin.Context and a zerolog.Logger as parameters,
16+
// and returns a zerolog.Logger. It is typically used to modify or enhance the logger
17+
// within the context of a Gin HTTP request.
1518
type Fn func(*gin.Context, zerolog.Logger) zerolog.Logger
1619

17-
// Skipper is a function to skip logs based on provided Context
20+
// Skipper defines a function to skip middleware. It takes a gin.Context as input
21+
// and returns a boolean indicating whether to skip the middleware for the given context.
1822
type Skipper func(c *gin.Context) bool
1923

20-
// Config defines the config for logger middleware
24+
// config holds the configuration for the logger middleware.
2125
type config struct {
26+
// logger is a function that defines the logging behavior.
2227
logger Fn
23-
// UTC a boolean stating whether to use UTC time zone or local.
24-
utc bool
25-
skipPath []string
28+
// utc is a boolean stating whether to use UTC time zone or local.
29+
utc bool
30+
// skipPath is a list of paths to be skipped from logging.
31+
skipPath []string
32+
// skipPathRegexps is a list of regular expressions to match paths to be skipped from logging.
2633
skipPathRegexps []*regexp.Regexp
27-
// skip is a Skipper that indicates which logs should not be written.
28-
// Optional.
34+
// skip is a Skipper that indicates which logs should not be written. Optional.
2935
skip Skipper
30-
// Output is a writer where logs are written.
31-
// Optional. Default value is gin.DefaultWriter.
36+
// output is a writer where logs are written. Optional. Default value is gin.DefaultWriter.
3237
output io.Writer
33-
// the log level used for request with status code < 400
38+
// defaultLevel is the log level used for requests with status code < 400.
3439
defaultLevel zerolog.Level
35-
// the log level used for request with status code between 400 and 499
40+
// clientErrorLevel is the log level used for requests with status code between 400 and 499.
3641
clientErrorLevel zerolog.Level
37-
// the log level used for request with status code >= 500
42+
// serverErrorLevel is the log level used for requests with status code >= 500.
3843
serverErrorLevel zerolog.Level
39-
// the log level to use for a specific path with status code < 400
44+
// pathLevels is a map of specific paths to log levels for requests with status code < 400.
4045
pathLevels map[string]zerolog.Level
4146
}
4247

4348
const loggerKey = "_gin-contrib/logger_"
4449

4550
var isTerm bool = isatty.IsTerminal(os.Stdout.Fd())
4651

47-
// SetLogger initializes the logging middleware.
52+
// SetLogger returns a gin.HandlerFunc (middleware) that logs requests using zerolog.
53+
// It accepts a variadic number of Option functions to customize the logger's behavior.
54+
//
55+
// The logger configuration includes:
56+
// - defaultLevel: the default logging level (default: zerolog.InfoLevel).
57+
// - clientErrorLevel: the logging level for client errors (default: zerolog.WarnLevel).
58+
// - serverErrorLevel: the logging level for server errors (default: zerolog.ErrorLevel).
59+
// - output: the output writer for the logger (default: gin.DefaultWriter).
60+
// - skipPath: a list of paths to skip logging.
61+
// - skipPathRegexps: a list of regular expressions to skip logging for matching paths.
62+
// - logger: a custom logger function to use instead of the default logger.
63+
//
64+
// The middleware logs the following request details:
65+
// - method: the HTTP method of the request.
66+
// - path: the URL path of the request.
67+
// - ip: the client's IP address.
68+
// - user_agent: the User-Agent header of the request.
69+
// - status: the HTTP status code of the response.
70+
// - latency: the time taken to process the request.
71+
// - body_size: the size of the response body.
72+
//
73+
// The logging level for each request is determined based on the response status code:
74+
// - clientErrorLevel for 4xx status codes.
75+
// - serverErrorLevel for 5xx status codes.
76+
// - defaultLevel for other status codes.
77+
// - Custom levels can be set for specific paths using the pathLevels configuration.
4878
func SetLogger(opts ...Option) gin.HandlerFunc {
4979
cfg := &config{
5080
defaultLevel: zerolog.InfoLevel,
@@ -156,13 +186,28 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
156186
}
157187
}
158188

159-
// ParseLevel converts a level string into a zerolog Level value.
160-
// returns an error if the input string does not match known values.
189+
// ParseLevel parses a string representation of a log level and returns the corresponding zerolog.Level.
190+
// It takes a single argument:
191+
// - levelStr: a string representing the log level (e.g., "debug", "info", "warn", "error").
192+
//
193+
// It returns:
194+
// - zerolog.Level: the parsed log level.
195+
// - error: an error if the log level string is invalid.
161196
func ParseLevel(levelStr string) (zerolog.Level, error) {
162197
return zerolog.ParseLevel(levelStr)
163198
}
164199

165-
// Get return the logger associated with a gin context
200+
// Get retrieves the zerolog.Logger instance from the given gin.Context.
201+
// It assumes that the logger has been previously set in the context with the key loggerKey.
202+
// If the logger is not found, it will panic.
203+
//
204+
// Parameters:
205+
//
206+
// c - the gin.Context from which to retrieve the logger.
207+
//
208+
// Returns:
209+
//
210+
// zerolog.Logger - the logger instance stored in the context.
166211
func Get(c *gin.Context) zerolog.Logger {
167212
return c.MustGet(loggerKey).(zerolog.Logger)
168213
}

0 commit comments

Comments
 (0)