1 Star 0 Fork 0

玟兵/gin-partner

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
logger.go 1.81 KB
一键复制 编辑 原始数据 按行查看 历史
玟兵 提交于 2024-08-13 15:33 . 细节优化
package partner
import (
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"os"
)
type Logger struct {
L *zap.Logger
lj *lumberjack.Logger
}
func NewLogger(filePath string, options ...zap.Option) *Logger {
cfg := zap.NewProductionEncoderConfig()
cfg.EncodeTime = zapcore.ISO8601TimeEncoder
lumberjackLogger := &lumberjack.Logger{
Filename: filePath,
MaxSize: 2,
MaxAge: 100,
MaxBackups: 10,
LocalTime: true,
Compress: false,
}
if len(options) == 0 {
options = append(options, zap.AddCaller(), zap.AddCallerSkip(1))
}
l := zap.New(
zapcore.NewTee(
zapcore.NewCore(
zapcore.NewJSONEncoder(cfg),
zapcore.AddSync(lumberjackLogger),
zap.LevelEnablerFunc(func(level zapcore.Level) bool {
return level != zapcore.DebugLevel
}),
),
zapcore.NewCore(
zapcore.NewConsoleEncoder(cfg),
zapcore.Lock(os.Stdout),
zap.LevelEnablerFunc(func(level zapcore.Level) bool {
return level == zapcore.DebugLevel
}),
),
),
options...,
)
return &Logger{
L: l,
lj: lumberjackLogger,
}
}
func (l *Logger) Close() {
_ = l.lj.Close()
_ = l.L.Sync()
}
func (l *Logger) Info(msg string, fields ...zap.Field) {
l.L.Info(msg, fields...)
}
func (l *Logger) Debug(msg string, fields ...zap.Field) {
if gin.Mode() == gin.ReleaseMode {
return
}
l.L.Debug(msg, fields...)
}
func (l *Logger) Warn(msg string, fields ...zap.Field) {
l.L.Warn(msg, fields...)
}
func (l *Logger) Error(msg string, fields ...zap.Field) {
l.L.Error(msg, fields...)
}
func (l *Logger) DPanic(msg string, fields ...zap.Field) {
l.L.DPanic(msg, fields...)
}
func (l *Logger) Panic(msg string, fields ...zap.Field) {
l.L.Panic(msg, fields...)
}
func (l *Logger) Fatal(msg string, fields ...zap.Field) {
l.L.Fatal(msg, fields...)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/binny_w/gin-partner.git
git@gitee.com:binny_w/gin-partner.git
binny_w
gin-partner
gin-partner
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385