代码拉取完成,页面将自动刷新
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...)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。