2022-05-14 13:04:01 +08:00
|
|
|
|
// Package wrapper...
|
|
|
|
|
//
|
|
|
|
|
// Description : http_gin 使用gin框架时的,记录日志
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 2021-01-03 3:43 下午
|
|
|
|
|
package wrapper
|
|
|
|
|
|
|
|
|
|
import (
|
2024-06-24 17:24:21 +08:00
|
|
|
|
"context"
|
2022-05-14 13:22:29 +08:00
|
|
|
|
"git.zhangdeman.cn/zhangdeman/logger"
|
2022-05-14 13:04:01 +08:00
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// NewGinLogger 使用gin框架记录日志
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 3:45 下午 2021/1/3
|
|
|
|
|
func NewGinLogger(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.Encoder, splitConfig *logger.RotateLogConfig, extractFieldList []string, skip int) (*Gin, error) {
|
|
|
|
|
var (
|
|
|
|
|
err error
|
|
|
|
|
l *zap.Logger
|
|
|
|
|
)
|
2022-05-14 13:22:29 +08:00
|
|
|
|
logConfList := []logger.SetLoggerOptionFunc{logger.WithEncoder(encoder), logger.WithCaller(), logger.WithCallerSkip(skip)}
|
2022-05-14 13:04:01 +08:00
|
|
|
|
if consoleOutput {
|
2022-05-14 13:22:29 +08:00
|
|
|
|
logConfList = append(logConfList, logger.WithConsoleOutput())
|
2022-05-14 13:04:01 +08:00
|
|
|
|
}
|
|
|
|
|
if l, err = logger.NewLogger(loggerLevel, splitConfig, logConfList...); nil != err {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &Gin{
|
|
|
|
|
loggerInstance: l,
|
|
|
|
|
extractFieldList: extractFieldList,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Gin 包装gin实例
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 3:59 下午 2021/1/3
|
|
|
|
|
type Gin struct {
|
|
|
|
|
loggerInstance *zap.Logger // zap 的日志实例
|
|
|
|
|
extractFieldList []string // 从gin中抽取的字段
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// formatFieldList 格式化日志field列表
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 4:13 下午 2021/1/3
|
2024-06-24 17:24:21 +08:00
|
|
|
|
func (gw *Gin) formatFieldList(ctx context.Context, inputFieldList []zap.Field) []zap.Field {
|
|
|
|
|
if nil == ctx {
|
|
|
|
|
ctx = context.Background()
|
|
|
|
|
}
|
2022-05-14 13:04:01 +08:00
|
|
|
|
if nil == inputFieldList {
|
|
|
|
|
inputFieldList = make([]zap.Field, 0)
|
|
|
|
|
}
|
2024-06-24 17:24:21 +08:00
|
|
|
|
// 自动扩充抽取字段,字段不存在的话,忽略掉
|
|
|
|
|
for _, extractField := range gw.extractFieldList {
|
|
|
|
|
inputFieldList = append(inputFieldList, zap.Any(extractField, ctx.Value(extractField)))
|
2022-05-14 13:04:01 +08:00
|
|
|
|
}
|
|
|
|
|
return inputFieldList
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Debug 日志
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 4:14 下午 2021/1/3
|
2024-06-24 17:24:21 +08:00
|
|
|
|
func (gw *Gin) Debug(ctx context.Context, msg string, field ...zap.Field) {
|
|
|
|
|
fieldList := gw.formatFieldList(ctx, field)
|
2022-05-14 13:04:01 +08:00
|
|
|
|
gw.loggerInstance.Debug(msg, fieldList...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Info 日志
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 4:28 下午 2021/1/3
|
2024-06-24 17:24:21 +08:00
|
|
|
|
func (gw *Gin) Info(ctx context.Context, msg string, field ...zap.Field) {
|
|
|
|
|
fieldList := gw.formatFieldList(ctx, field)
|
2022-05-14 13:04:01 +08:00
|
|
|
|
gw.loggerInstance.Info(msg, fieldList...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Warn 日志
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 4:29 下午 2021/1/3
|
2024-06-24 17:24:21 +08:00
|
|
|
|
func (gw *Gin) Warn(ctx context.Context, msg string, field ...zap.Field) {
|
|
|
|
|
fieldList := gw.formatFieldList(ctx, field)
|
2022-05-14 13:04:01 +08:00
|
|
|
|
gw.loggerInstance.Warn(msg, fieldList...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Error 日志
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 4:29 下午 2021/1/3
|
2024-06-24 17:24:21 +08:00
|
|
|
|
func (gw *Gin) Error(ctx context.Context, msg string, field ...zap.Field) {
|
|
|
|
|
fieldList := gw.formatFieldList(ctx, field)
|
2022-05-14 13:04:01 +08:00
|
|
|
|
gw.loggerInstance.Error(msg, fieldList...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Panic 日志
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 4:29 下午 2021/1/3
|
2024-06-24 17:24:21 +08:00
|
|
|
|
func (gw *Gin) Panic(ctx context.Context, msg string, field ...zap.Field) {
|
|
|
|
|
fieldList := gw.formatFieldList(ctx, field)
|
2022-05-14 13:04:01 +08:00
|
|
|
|
gw.loggerInstance.Panic(msg, fieldList...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DPanic 日志
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 4:30 下午 2021/1/3
|
2024-06-24 17:24:21 +08:00
|
|
|
|
func (gw *Gin) DPanic(ctx context.Context, msg string, field ...zap.Field) {
|
|
|
|
|
fieldList := gw.formatFieldList(ctx, field)
|
2022-05-14 13:04:01 +08:00
|
|
|
|
gw.loggerInstance.DPanic(msg, fieldList...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetZapLoggerInstance 获取zap日志实例
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 2021/01/03 22:56:47
|
|
|
|
|
func (gw *Gin) GetZapLoggerInstance() *zap.Logger {
|
|
|
|
|
return gw.loggerInstance
|
|
|
|
|
}
|