@@ -47,8 +47,15 @@ type LoggerConfig struct {
4747 // SkipPaths is an url path array which logs are not written.
4848 // Optional.
4949 SkipPaths []string
50+
51+ // Skip is a Skipper that indicates which logs should not be written.
52+ // Optional.
53+ Skip Skipper
5054}
5155
56+ // Skipper is a function to skip logs based on provided Context
57+ type Skipper func (c * Context ) bool
58+
5259// LogFormatter gives the signature of the formatter function passed to LoggerWithFormatter
5360type LogFormatter func (params LogFormatterParams ) string
5461
@@ -241,32 +248,34 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
241248 // Process request
242249 c .Next ()
243250
244- // Log only when path is not being skipped
245- if _ , ok := skip [path ]; ! ok {
246- param := LogFormatterParams {
247- Request : c .Request ,
248- isTerm : isTerm ,
249- Keys : c .Keys ,
250- }
251-
252- // Stop timer
253- param .TimeStamp = time .Now ()
254- param .Latency = param .TimeStamp .Sub (start )
251+ // Log only when it is not being skipped
252+ if _ , ok := skip [path ]; ok || (conf .Skip != nil && conf .Skip (c )) {
253+ return
254+ }
255255
256- param .ClientIP = c .ClientIP ()
257- param .Method = c .Request .Method
258- param .StatusCode = c .Writer .Status ()
259- param .ErrorMessage = c .Errors .ByType (ErrorTypePrivate ).String ()
256+ param := LogFormatterParams {
257+ Request : c .Request ,
258+ isTerm : isTerm ,
259+ Keys : c .Keys ,
260+ }
260261
261- param .BodySize = c .Writer .Size ()
262+ // Stop timer
263+ param .TimeStamp = time .Now ()
264+ param .Latency = param .TimeStamp .Sub (start )
262265
263- if raw != "" {
264- path = path + "?" + raw
265- }
266+ param .ClientIP = c .ClientIP ()
267+ param .Method = c .Request .Method
268+ param .StatusCode = c .Writer .Status ()
269+ param .ErrorMessage = c .Errors .ByType (ErrorTypePrivate ).String ()
266270
267- param .Path = path
271+ param .BodySize = c . Writer . Size ()
268272
269- fmt .Fprint (out , formatter (param ))
273+ if raw != "" {
274+ path = path + "?" + raw
270275 }
276+
277+ param .Path = path
278+
279+ fmt .Fprint (out , formatter (param ))
271280 }
272281}
0 commit comments