36 lines
790 B
Go
36 lines
790 B
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
lib "git.sr.ht/~cco/go-scopes"
|
|
"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, fmt.Errorf("gin error"))
|
|
} 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).
|
|
Str("url", c.Request.URL.String()).
|
|
// Dur("resp_time", time.Since(t)).
|
|
Int("status", status).
|
|
Str("client_ip", c.ClientIP()).Msg(msg)
|
|
}
|
|
}
|