Skip to content

Commit 75a0ac1

Browse files
authored
refactor: refactor initialization and simplify logic for clarity (#102)
- 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]>
1 parent 56e87fc commit 75a0ac1

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

logger.go

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -87,30 +87,24 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
8787
output: gin.DefaultWriter,
8888
}
8989

90-
// Loop through each option
90+
// Apply each option to the config
9191
for _, o := range opts {
92-
// Call the option giving the instantiated
9392
o.apply(cfg)
9493
}
9594

96-
var skip map[string]struct{}
97-
if length := len(cfg.skipPath); length > 0 {
98-
skip = make(map[string]struct{}, length)
99-
for _, path := range cfg.skipPath {
100-
skip[path] = struct{}{}
101-
}
95+
// Create a set of paths to skip logging
96+
skip := make(map[string]struct{}, len(cfg.skipPath))
97+
for _, path := range cfg.skipPath {
98+
skip[path] = struct{}{}
10299
}
103100

101+
// Initialize the base logger
104102
l := zerolog.New(cfg.output).
105-
Output(
106-
zerolog.ConsoleWriter{
107-
Out: cfg.output,
108-
NoColor: !isTerm,
109-
},
110-
).
103+
Output(zerolog.ConsoleWriter{Out: cfg.output, NoColor: !isTerm}).
111104
With().
112105
Timestamp().
113106
Logger()
107+
114108
return func(c *gin.Context) {
115109
rl := l
116110
if cfg.logger != nil {
@@ -119,36 +113,32 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
119113

120114
start := time.Now()
121115
path := c.Request.URL.Path
122-
raw := c.Request.URL.RawQuery
123-
if raw != "" {
124-
path = path + "?" + raw
116+
if raw := c.Request.URL.RawQuery; raw != "" {
117+
path += "?" + raw
125118
}
126119

127120
track := true
128-
129121
if _, ok := skip[path]; ok || (cfg.skip != nil && cfg.skip(c)) {
130122
track = false
131123
}
132124

133-
if track && len(cfg.skipPathRegexps) > 0 {
125+
if track {
134126
for _, reg := range cfg.skipPathRegexps {
135-
if !reg.MatchString(path) {
136-
continue
127+
if reg.MatchString(path) {
128+
track = false
129+
break
137130
}
138-
139-
track = false
140-
break
141131
}
142132
}
143133

144-
// Use a separate logger to save to the context, as we want to avoid mutating the original logger.
145134
contextLogger := rl
146135
if track {
147136
contextLogger = rl.With().
148137
Str("method", c.Request.Method).
149138
Str("path", path).
150139
Str("ip", c.ClientIP()).
151-
Str("user_agent", c.Request.UserAgent()).Logger()
140+
Str("user_agent", c.Request.UserAgent()).
141+
Logger()
152142
}
153143
c.Set(loggerKey, contextLogger)
154144

@@ -179,9 +169,11 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
179169
default:
180170
evt = rl.WithLevel(cfg.defaultLevel).Ctx(c)
181171
}
172+
182173
if cfg.context != nil {
183174
evt = cfg.context(c, evt)
184175
}
176+
185177
evt.
186178
Int("status", c.Writer.Status()).
187179
Str("method", c.Request.Method).

0 commit comments

Comments
 (0)