cleaned up LogMiddleware
Dieser Commit ist enthalten in:
Ursprung
9dd2e49954
Commit
dcbfbf9ea8
|
@ -8,7 +8,7 @@ import (
|
|||
"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) {
|
||||
var xid log.XID
|
||||
var tmp interface{}
|
||||
|
@ -20,7 +20,7 @@ func LogMiddleware(c *gin.Context) {
|
|||
xid = tmp.(log.XID)
|
||||
}
|
||||
defer func() {
|
||||
var entry *log.Entry
|
||||
var entry = log.Info()
|
||||
int := recover()
|
||||
if int != nil {
|
||||
err := int.(error)
|
||||
|
@ -28,12 +28,11 @@ func LogMiddleware(c *gin.Context) {
|
|||
c.AbortWithStatus(http.StatusInternalServerError)
|
||||
entry = log.Error().Err(err).Int("statuscode", 500)
|
||||
} else {
|
||||
if c.Writer.Status() >= 400 {
|
||||
statuscode := c.Writer.Status()
|
||||
if statuscode >= 400 {
|
||||
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")
|
||||
}()
|
||||
|
|
Laden…
In neuem Issue referenzieren