33 lines
715 B
Go
33 lines
715 B
Go
package server
|
|
|
|
import (
|
|
"git.sr.ht/~cco/go-scopes/lib"
|
|
"git.sr.ht/~cco/go-scopes/logging"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
func Logger(ctx lib.Context) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
// t := time.Now()
|
|
c.Next()
|
|
msg := c.Errors.String()
|
|
if msg == "" {
|
|
msg = "request"
|
|
}
|
|
var evt *zerolog.Event
|
|
status := c.Writer.Status()
|
|
if status >= 500 {
|
|
evt = logging.Error(ctx)
|
|
} else if status >= 400 {
|
|
evt = logging.Warn(ctx)
|
|
} else {
|
|
evt = logging.Info(ctx)
|
|
}
|
|
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)
|
|
}
|
|
}
|