-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (64 loc) · 1.82 KB
/
main.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"os"
"time"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main() {
//替换default的设置
//r := gin.Default()
r := gin.New()
//使用自定义的logger
logger := InitLogger()
//使用别人封装好的gin zap日志库
r.Use(ZapLogger(logger), gin.Recovery())
//r.Use(ginzap.Ginzap(logger, time.RFC3339, true))
//注册接口并运行服务器
r.GET("/hello", func(context *gin.Context) {
logger.Debug(`context.String(200, "hello gin and zap") success`,
zap.Any("context", context),
)
context.String(200, "hello gin and zap")
})
r.Run()
}
//初始化日志对象
func InitLogger() *zap.Logger {
//准备配置参数
enc := zapcore.NewConsoleEncoder(zap.NewProductionEncoderConfig())
file, _ := os.Create("./zap.log")
ws := zapcore.AddSync(file)
enab := zapcore.DebugLevel
//创建日志对象
core := zapcore.NewCore(enc, ws, enab)
logger := zap.New(core)
//sugarLog := logger.Sugar()
return logger
}
//中间件要实现handleFunc 类型
// HandlerFunc defines the handler used by gin middleware as return value.
//type HandlerFunc func(*Context)
func ZapLogger(zapLogger *zap.Logger) gin.HandlerFunc {
return func(context *gin.Context) {
//记录参数收集
start := time.Now()
path := context.Request.URL.Path
query := context.Request.URL.RawQuery
context.Next()
cost := time.Since(start)
//日志信息写入
zapLogger.Info(path,
zap.String("writer", "ZapLogger"),
zap.Int("status", context.Writer.Status()),
zap.String("method", context.Request.Method),
zap.String("path", path),
zap.String("query", query),
zap.String("ip", context.ClientIP()),
zap.String("user-agent", context.Request.UserAgent()),
zap.String("errors", context.Errors.ByType(gin.ErrorTypePrivate).String()),
zap.Duration("cost", cost),
)
}
}