forked from micro-plat/hydra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
33 lines (29 loc) · 1.04 KB
/
logging.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
33
package middleware
import (
"strings"
"time"
"github.com/micro-plat/hydra/conf"
"github.com/micro-plat/hydra/servers/pkg/dispatcher"
"github.com/micro-plat/lib4go/logger"
)
//Logging 记录日志
func Logging(conf *conf.MetadataConf) dispatcher.HandlerFunc {
return func(ctx *dispatcher.Context) {
start := time.Now()
setStartTime(ctx)
p := ctx.Request.GetService()
uuid := getUUID(ctx)
setUUID(ctx, uuid)
log := logger.GetSession(conf.Name, uuid, "biz", strings.Replace(strings.Trim(ctx.Request.GetService(), "/"), "/", "_", -1))
log.Info(conf.Type+".request:", conf.Name, ctx.Request.GetMethod(), p, "from", ctx.ClientIP())
setLogger(ctx, log)
ctx.Next()
v, _ := getResponseRaw(ctx)
statusCode := ctx.Writer.Status()
if statusCode >= 200 && statusCode < 400 {
log.Info(conf.Type+".response:", conf.Name, ctx.Request.GetMethod(), p, statusCode, getExt(ctx), time.Since(start), v)
} else {
log.Error(conf.Type+".response:", conf.Name, ctx.Request.GetMethod(), p, statusCode, getExt(ctx), time.Since(start), v)
}
}
}