1
0
Fork 0

cleaned up LogMiddleware

Dieser Commit ist enthalten in:
Sebastian Tobie 2021-01-10 15:15:29 +01:00
Ursprung 9dd2e49954
Commit dcbfbf9ea8
1 geänderte Dateien mit 5 neuen und 6 gelöschten Zeilen

Datei anzeigen

@ -8,7 +8,7 @@ import (
"github.com/phuslu/log" "github.com/phuslu/log"
) )
// LogMiddleware is an middleware to log requests to phuslu/log // LogMiddleware is an middleware to log requests to phuslu/log and catches panics
func LogMiddleware(c *gin.Context) { func LogMiddleware(c *gin.Context) {
var xid log.XID var xid log.XID
var tmp interface{} var tmp interface{}
@ -20,7 +20,7 @@ func LogMiddleware(c *gin.Context) {
xid = tmp.(log.XID) xid = tmp.(log.XID)
} }
defer func() { defer func() {
var entry *log.Entry var entry = log.Info()
int := recover() int := recover()
if int != nil { if int != nil {
err := int.(error) err := int.(error)
@ -28,12 +28,11 @@ func LogMiddleware(c *gin.Context) {
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 {
if c.Writer.Status() >= 400 { statuscode := c.Writer.Status()
if statuscode >= 400 {
entry = log.Error() entry = log.Error()
} else {
entry = log.Info()
} }
entry = entry.Int("statuscode", c.Writer.Status()) entry = entry.Int("statuscode", statuscode)
} }
entry.Int64("goroutine", log.Goid()).Xid("ID", xid).Msg("Request") entry.Int64("goroutine", log.Goid()).Xid("ID", xid).Msg("Request")
}() }()