-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
32 lines (26 loc) · 895 Bytes
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package middleware
import (
"time"
log "github.com/sirupsen/logrus"
"github.com/valyala/fasthttp"
)
type Logger struct {
}
func NewLogger() *Logger {
return &Logger{}
}
func (l *Logger) Handle() func(next fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(h fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
startTime := time.Now()
h(ctx)
if ctx.Response.StatusCode() < 400 {
log.Infof("user access code %d time %v, method %s path %s client-real-ip %s",
ctx.Response.StatusCode(), time.Since(startTime), string(ctx.Method()), ctx.Path(), ctx.Request.Header.Peek("X-Real-IP"))
} else {
log.Warnf("user access code %d time %v, method %s path %s client-real-ip %s",
ctx.Response.StatusCode(), time.Since(startTime), string(ctx.Method()), ctx.Path(), ctx.Request.Header.Peek("X-Real-IP"))
}
}
}
}