1
0
Fork 0

the LogMiddleware now produces less severe log enttries, which should keep the log more calm

Dieser Commit ist enthalten in:
Sebastian Tobie 2021-11-07 21:40:44 +01:00
Ursprung 6d43861ffc
Commit 327adffebf
1 geänderte Dateien mit 11 neuen und 6 gelöschten Zeilen

Datei anzeigen

@ -19,19 +19,24 @@ func LogMiddleware(c *gin.Context) {
var xid = log.NewXIDWithTime(time.Now().UnixNano()) var xid = log.NewXIDWithTime(time.Now().UnixNano())
c.Set("xid", xid) c.Set("xid", xid)
defer func() { defer func() {
var entry = log.Info() var entry *log.Entry
int := recover() interrupt := recover()
if int != nil { if interrupt != nil {
err := int.(error) err := interrupt.(error)
c.Header("requestid", xid.String()) c.Header("requestid", xid.String())
c.AbortWithStatus(http.StatusInternalServerError) c.AbortWithStatus(http.StatusInternalServerError)
entry = log.Error().Err(err).Int("statuscode", 500) entry = log.Error().Err(err).Int("statuscode", 500)
} else { } else {
statuscode := c.Writer.Status() statuscode := c.Writer.Status()
if statuscode >= 400 { switch {
case statuscode >= 400 && statuscode < 500:
entry = log.Warn()
case statuscode >= 500:
entry = log.Error() entry = log.Error()
default:
entry = log.Info()
} }
entry = entry.Int("statuscode", statuscode) entry.Int("statuscode", statuscode)
} }
entry.Int64("goroutine", log.Goid()).Xid("ID", xid).Msg("Request") entry.Int64("goroutine", log.Goid()).Xid("ID", xid).Msg("Request")
}() }()