Skip to content

Commit

Permalink
refactor: refactor initialization and simplify logic for clarity (#102)
Browse files Browse the repository at this point in the history
- Update comments to clarify the purpose of code blocks
- Simplify the initialization of the `skip` map
- Consolidate the logger output initialization into a single line
- Refactor URL query handling to use a more concise format
- Simplify the logic for skipping paths based on regex matches
- Ensure the `track` variable is properly scoped and used
- Break down the logger context initialization into smaller steps for clarity

Signed-off-by: appleboy <[email protected]>
  • Loading branch information
appleboy authored Dec 28, 2024
1 parent 56e87fc commit 75a0ac1
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,24 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
output: gin.DefaultWriter,
}

// Loop through each option
// Apply each option to the config
for _, o := range opts {
// Call the option giving the instantiated
o.apply(cfg)
}

var skip map[string]struct{}
if length := len(cfg.skipPath); length > 0 {
skip = make(map[string]struct{}, length)
for _, path := range cfg.skipPath {
skip[path] = struct{}{}
}
// Create a set of paths to skip logging
skip := make(map[string]struct{}, len(cfg.skipPath))
for _, path := range cfg.skipPath {
skip[path] = struct{}{}
}

// Initialize the base logger
l := zerolog.New(cfg.output).
Output(
zerolog.ConsoleWriter{
Out: cfg.output,
NoColor: !isTerm,
},
).
Output(zerolog.ConsoleWriter{Out: cfg.output, NoColor: !isTerm}).
With().
Timestamp().
Logger()

return func(c *gin.Context) {
rl := l
if cfg.logger != nil {
Expand All @@ -119,36 +113,32 @@ func SetLogger(opts ...Option) gin.HandlerFunc {

start := time.Now()
path := c.Request.URL.Path
raw := c.Request.URL.RawQuery
if raw != "" {
path = path + "?" + raw
if raw := c.Request.URL.RawQuery; raw != "" {
path += "?" + raw
}

track := true

if _, ok := skip[path]; ok || (cfg.skip != nil && cfg.skip(c)) {
track = false
}

if track && len(cfg.skipPathRegexps) > 0 {
if track {
for _, reg := range cfg.skipPathRegexps {
if !reg.MatchString(path) {
continue
if reg.MatchString(path) {
track = false
break
}

track = false
break
}
}

// Use a separate logger to save to the context, as we want to avoid mutating the original logger.
contextLogger := rl
if track {
contextLogger = rl.With().
Str("method", c.Request.Method).
Str("path", path).
Str("ip", c.ClientIP()).
Str("user_agent", c.Request.UserAgent()).Logger()
Str("user_agent", c.Request.UserAgent()).
Logger()
}
c.Set(loggerKey, contextLogger)

Expand Down Expand Up @@ -179,9 +169,11 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
default:
evt = rl.WithLevel(cfg.defaultLevel).Ctx(c)
}

if cfg.context != nil {
evt = cfg.context(c, evt)
}

evt.
Int("status", c.Writer.Status()).
Str("method", c.Request.Method).
Expand Down

0 comments on commit 75a0ac1

Please sign in to comment.