mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 13:22:09 +08:00
do not call time.now() if logging should be skipped
This commit is contained in:
parent
99e44da2e3
commit
615c50ff2f
55
logger.go
55
logger.go
@ -238,6 +238,12 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return func(c *Context) {
|
return func(c *Context) {
|
||||||
|
// Log only when it is not being skipped
|
||||||
|
if _, ok := skip[c.Request.URL.Path]; ok || (conf.Skip != nil && conf.Skip(c)) {
|
||||||
|
c.Next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Start timer
|
// Start timer
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
path := c.Request.URL.Path
|
path := c.Request.URL.Path
|
||||||
@ -246,32 +252,29 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
|
|||||||
// Process request
|
// Process request
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|
||||||
// Log only when it is not being skipped
|
param := LogFormatterParams{
|
||||||
if _, ok := skip[path]; !ok && conf.Skip != nil && !conf.Skip(c) {
|
Request: c.Request,
|
||||||
param := LogFormatterParams{
|
isTerm: isTerm,
|
||||||
Request: c.Request,
|
Keys: c.Keys,
|
||||||
isTerm: isTerm,
|
|
||||||
Keys: c.Keys,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop timer
|
|
||||||
param.TimeStamp = time.Now()
|
|
||||||
param.Latency = param.TimeStamp.Sub(start)
|
|
||||||
|
|
||||||
param.ClientIP = c.ClientIP()
|
|
||||||
param.Method = c.Request.Method
|
|
||||||
param.StatusCode = c.Writer.Status()
|
|
||||||
param.ErrorMessage = c.Errors.ByType(ErrorTypePrivate).String()
|
|
||||||
|
|
||||||
param.BodySize = c.Writer.Size()
|
|
||||||
|
|
||||||
if raw != "" {
|
|
||||||
path = path + "?" + raw
|
|
||||||
}
|
|
||||||
|
|
||||||
param.Path = path
|
|
||||||
|
|
||||||
fmt.Fprint(out, formatter(param))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Stop timer
|
||||||
|
param.TimeStamp = time.Now()
|
||||||
|
param.Latency = param.TimeStamp.Sub(start)
|
||||||
|
|
||||||
|
param.ClientIP = c.ClientIP()
|
||||||
|
param.Method = c.Request.Method
|
||||||
|
param.StatusCode = c.Writer.Status()
|
||||||
|
param.ErrorMessage = c.Errors.ByType(ErrorTypePrivate).String()
|
||||||
|
|
||||||
|
param.BodySize = c.Writer.Size()
|
||||||
|
|
||||||
|
if raw != "" {
|
||||||
|
path = path + "?" + raw
|
||||||
|
}
|
||||||
|
|
||||||
|
param.Path = path
|
||||||
|
|
||||||
|
fmt.Fprint(out, formatter(param))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user