2021-02-27 16:31:12 +08:00
|
|
|
|
// Package wrapper...
|
|
|
|
|
//
|
|
|
|
|
// Description : http_gin 使用gin框架时的,记录日志
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
|
//
|
|
|
|
|
// Date : 2021-01-03 3:43 下午
|
|
|
|
|
package wrapper
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/go-developer/gopkg/logger"
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// NewGinWrapperLogger 使用gin框架记录日志
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
|
//
|
|
|
|
|
// Date : 3:45 下午 2021/1/3
|
|
|
|
|
func NewGinWrapperLogger(loggerLevel zapcore.Level, consoleOutput bool, encoder zapcore.Encoder, splitConfig *logger.RotateLogConfig, extractFieldList []string) (*GinWrapper, error) {
|
|
|
|
|
var (
|
|
|
|
|
err error
|
|
|
|
|
l *zap.Logger
|
|
|
|
|
)
|
|
|
|
|
if l, err = logger.NewLogger(loggerLevel, consoleOutput, encoder, splitConfig); nil != err {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &GinWrapper{
|
|
|
|
|
loggerInstance: l,
|
|
|
|
|
extractFieldList: extractFieldList,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GinWrapper 包装gin实例
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
|
//
|
|
|
|
|
// Date : 3:59 下午 2021/1/3
|
|
|
|
|
type GinWrapper struct {
|
2021-02-27 16:37:34 +08:00
|
|
|
|
loggerInstance *zap.Logger // zap 的日志实例
|
|
|
|
|
extractFieldList []string // 从gin中抽取的字段
|
2021-02-27 16:31:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// formatFieldList 格式化日志field列表
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
|
//
|
|
|
|
|
// Date : 4:13 下午 2021/1/3
|
2021-02-27 16:37:34 +08:00
|
|
|
|
func (gw *GinWrapper) formatFieldList(ginCtx *gin.Context, inputFieldList []zap.Field) []zap.Field {
|
2021-02-27 16:31:12 +08:00
|
|
|
|
if nil == inputFieldList {
|
|
|
|
|
inputFieldList = make([]zap.Field, 0)
|
|
|
|
|
}
|
2021-02-27 16:37:34 +08:00
|
|
|
|
if nil != ginCtx {
|
2021-02-27 16:31:12 +08:00
|
|
|
|
// 自动扩充抽取字段,字段不存在的话,忽略掉
|
|
|
|
|
for _, extractField := range gw.extractFieldList {
|
2021-02-27 16:37:34 +08:00
|
|
|
|
if v, exist := ginCtx.Get(extractField); exist {
|
2021-02-27 16:31:12 +08:00
|
|
|
|
byteData, _ := json.Marshal(v)
|
|
|
|
|
inputFieldList = append(inputFieldList, zap.String(extractField, string(byteData)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return inputFieldList
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Debug 日志
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
|
//
|
|
|
|
|
// Date : 4:14 下午 2021/1/3
|
2021-02-27 16:37:34 +08:00
|
|
|
|
func (gw *GinWrapper) Debug(ginCtx *gin.Context, msg string, field ...zap.Field) {
|
|
|
|
|
fieldList := gw.formatFieldList(ginCtx, field)
|
2021-02-27 16:31:12 +08:00
|
|
|
|
gw.loggerInstance.Debug(msg, fieldList...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Info 日志
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
|
//
|
|
|
|
|
// Date : 4:28 下午 2021/1/3
|
2021-02-27 16:37:34 +08:00
|
|
|
|
func (gw *GinWrapper) Info(ginCtx *gin.Context, msg string, field ...zap.Field) {
|
|
|
|
|
fieldList := gw.formatFieldList(ginCtx, field)
|
2021-02-27 16:31:12 +08:00
|
|
|
|
gw.loggerInstance.Info(msg, fieldList...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Warn 日志
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
|
//
|
|
|
|
|
// Date : 4:29 下午 2021/1/3
|
2021-02-27 16:37:34 +08:00
|
|
|
|
func (gw *GinWrapper) Warn(ginCtx *gin.Context, msg string, field ...zap.Field) {
|
|
|
|
|
fieldList := gw.formatFieldList(ginCtx, field)
|
2021-02-27 16:31:12 +08:00
|
|
|
|
gw.loggerInstance.Warn(msg, fieldList...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Error 日志
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
|
//
|
|
|
|
|
// Date : 4:29 下午 2021/1/3
|
2021-02-27 16:37:34 +08:00
|
|
|
|
func (gw *GinWrapper) Error(ginCtx *gin.Context, msg string, field ...zap.Field) {
|
|
|
|
|
fieldList := gw.formatFieldList(ginCtx, field)
|
2021-02-27 16:31:12 +08:00
|
|
|
|
gw.loggerInstance.Error(msg, fieldList...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Panic 日志
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
|
//
|
|
|
|
|
// Date : 4:29 下午 2021/1/3
|
2021-02-27 16:37:34 +08:00
|
|
|
|
func (gw *GinWrapper) Panic(ginCtx *gin.Context, msg string, field ...zap.Field) {
|
|
|
|
|
fieldList := gw.formatFieldList(ginCtx, field)
|
2021-02-27 16:31:12 +08:00
|
|
|
|
gw.loggerInstance.Panic(msg, fieldList...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DPanic 日志
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
|
//
|
|
|
|
|
// Date : 4:30 下午 2021/1/3
|
2021-02-27 16:37:34 +08:00
|
|
|
|
func (gw *GinWrapper) DPanic(ginCtx *gin.Context, msg string, field ...zap.Field) {
|
|
|
|
|
fieldList := gw.formatFieldList(ginCtx, field)
|
2021-02-27 16:31:12 +08:00
|
|
|
|
gw.loggerInstance.DPanic(msg, fieldList...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetZapLoggerInstance 获取zap日志实例
|
|
|
|
|
//
|
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
|
//
|
|
|
|
|
// Date : 2021/01/03 22:56:47
|
|
|
|
|
func (gw *GinWrapper) GetZapLoggerInstance() *zap.Logger {
|
|
|
|
|
return gw.loggerInstance
|
|
|
|
|
}
|