server: minor improvements of logger middleware

This commit is contained in:
Helmut Merz 2023-07-01 19:33:13 +02:00
parent 4e06072d96
commit ea91a0486e

View file

@ -9,30 +9,23 @@ import (
func Logger(ctx lib.Context) gin.HandlerFunc {
return func(c *gin.Context) {
// before request
// t := time.Now()
path := c.Request.URL.Path
raw := c.Request.URL.RawQuery
if raw != "" {
path = path + "?" + raw
}
c.Next()
// after request
msg := c.Errors.String()
if msg == "" {
msg = "request"
}
status := c.Writer.Status()
var evt *zerolog.Event
switch {
case status >= 500:
status := c.Writer.Status()
if status >= 500 {
evt = logging.Error(ctx)
case status >= 400:
} else if status >= 400 {
evt = logging.Warn(ctx)
default:
} else {
evt = logging.Info(ctx)
}
evt.Str("method", c.Request.Method).Str("path", path).
evt.Str("method", c.Request.Method).
Str("path", c.Request.URL.Path).
// Dur("resp_time", time.Since(t)).
Int("status", status).
Str("client_ip", c.ClientIP()).Msg(msg)