Skip to content

Commit 77133ee

Browse files
committed
refactor: refactor logging and initialization for improved modularity
- Simplify the initialization of the `isTerm` variable. - Replace inline logging skip logic with a new function `shouldSkipLogging`. - Replace inline logging event handling with a new function `getLogEvent`. - Add `shouldSkipLogging` function to determine if logging should be skipped based on path and configuration. - Add `getLogEvent` function to encapsulate logic for determining the appropriate log event based on the HTTP status and configuration. Signed-off-by: appleboy <[email protected]>
1 parent 84d9845 commit 77133ee

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

logger.go

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type config struct {
5353

5454
const loggerKey = "_gin-contrib/logger_"
5555

56-
var isTerm bool = isatty.IsTerminal(os.Stdout.Fd())
56+
var isTerm = isatty.IsTerminal(os.Stdout.Fd())
5757

5858
// SetLogger returns a gin.HandlerFunc (middleware) that logs requests using zerolog.
5959
// It accepts a variadic number of Option functions to customize the logger's behavior.
@@ -120,19 +120,7 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
120120
path += "?" + raw
121121
}
122122

123-
track := true
124-
if _, ok := skip[path]; ok || (cfg.skip != nil && cfg.skip(c)) {
125-
track = false
126-
}
127-
128-
if track {
129-
for _, reg := range cfg.skipPathRegexps {
130-
if reg.MatchString(path) {
131-
track = false
132-
break
133-
}
134-
}
135-
}
123+
track := !shouldSkipLogging(path, skip, cfg, c)
136124

137125
contextLogger := rl
138126
if track {
@@ -158,19 +146,7 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
158146
cfg.message += " with errors: " + c.Errors.String()
159147
}
160148

161-
var evt *zerolog.Event
162-
level, hasLevel := cfg.pathLevels[path]
163-
164-
switch {
165-
case c.Writer.Status() >= http.StatusBadRequest && c.Writer.Status() < http.StatusInternalServerError:
166-
evt = rl.WithLevel(cfg.clientErrorLevel).Ctx(c)
167-
case c.Writer.Status() >= http.StatusInternalServerError:
168-
evt = rl.WithLevel(cfg.serverErrorLevel).Ctx(c)
169-
case hasLevel:
170-
evt = rl.WithLevel(level).Ctx(c)
171-
default:
172-
evt = rl.WithLevel(cfg.defaultLevel).Ctx(c)
173-
}
149+
evt := getLogEvent(rl, cfg, c, path)
174150

175151
if cfg.context != nil {
176152
evt = cfg.context(c, evt)
@@ -200,7 +176,33 @@ func ParseLevel(levelStr string) (zerolog.Level, error) {
200176
return zerolog.ParseLevel(levelStr)
201177
}
202178

203-
// Get retrieves the zerolog.Logger instance from the given gin.Context.
179+
func shouldSkipLogging(path string, skip map[string]struct{}, cfg *config, c *gin.Context) bool {
180+
if _, ok := skip[path]; ok || (cfg.skip != nil && cfg.skip(c)) {
181+
return true
182+
}
183+
for _, reg := range cfg.skipPathRegexps {
184+
if reg.MatchString(path) {
185+
return true
186+
}
187+
}
188+
return false
189+
}
190+
191+
func getLogEvent(rl zerolog.Logger, cfg *config, c *gin.Context, path string) *zerolog.Event {
192+
level, hasLevel := cfg.pathLevels[path]
193+
switch {
194+
case c.Writer.Status() >= http.StatusBadRequest && c.Writer.Status() < http.StatusInternalServerError:
195+
return rl.WithLevel(cfg.clientErrorLevel).Ctx(c)
196+
case c.Writer.Status() >= http.StatusInternalServerError:
197+
return rl.WithLevel(cfg.serverErrorLevel).Ctx(c)
198+
case hasLevel:
199+
return rl.WithLevel(level).Ctx(c)
200+
default:
201+
return rl.WithLevel(cfg.defaultLevel).Ctx(c)
202+
}
203+
}
204+
205+
// GetLogger retrieves the zerolog.Logger instance from the given gin.Context.
204206
// It assumes that the logger has been previously set in the context with the key loggerKey.
205207
// If the logger is not found, it will panic.
206208
//

0 commit comments

Comments
 (0)