LoggerWithConfig instance a Logger middleware with config.
(conf LoggerConfig)
| 243 | |
| 244 | // LoggerWithConfig instance a Logger middleware with config. |
| 245 | func LoggerWithConfig(conf LoggerConfig) HandlerFunc { |
| 246 | formatter := conf.Formatter |
| 247 | if formatter == nil { |
| 248 | formatter = defaultLogFormatter |
| 249 | } |
| 250 | |
| 251 | out := conf.Output |
| 252 | if out == nil { |
| 253 | out = DefaultWriter |
| 254 | } |
| 255 | |
| 256 | notlogged := conf.SkipPaths |
| 257 | |
| 258 | isTerm := true |
| 259 | |
| 260 | if w, ok := out.(*os.File); !ok || os.Getenv("TERM") == "dumb" || |
| 261 | (!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd())) { |
| 262 | isTerm = false |
| 263 | } |
| 264 | |
| 265 | var skip map[string]struct{} |
| 266 | |
| 267 | if length := len(notlogged); length > 0 { |
| 268 | skip = make(map[string]struct{}, length) |
| 269 | |
| 270 | for _, path := range notlogged { |
| 271 | skip[path] = struct{}{} |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | return func(c *Context) { |
| 276 | // Start timer |
| 277 | start := time.Now() |
| 278 | path := c.Request.URL.Path |
| 279 | raw := c.Request.URL.RawQuery |
| 280 | |
| 281 | // Process request |
| 282 | c.Next() |
| 283 | |
| 284 | // Log only when it is not being skipped |
| 285 | if _, ok := skip[path]; ok || (conf.Skip != nil && conf.Skip(c)) { |
| 286 | return |
| 287 | } |
| 288 | |
| 289 | param := LogFormatterParams{ |
| 290 | Request: c.Request, |
| 291 | isTerm: isTerm, |
| 292 | Keys: c.Keys, |
| 293 | } |
| 294 | |
| 295 | // Stop timer |
| 296 | param.TimeStamp = time.Now() |
| 297 | param.Latency = param.TimeStamp.Sub(start) |
| 298 | |
| 299 | param.ClientIP = c.ClientIP() |
| 300 | param.Method = c.Request.Method |
| 301 | param.StatusCode = c.Writer.Status() |
| 302 | param.ErrorMessage = c.Errors.ByType(ErrorTypePrivate).String() |