You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
golog/zap.go

186 lines
5.2 KiB

2 years ago
package golog
import (
2 years ago
"context"
2 years ago
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"time"
)
2 years ago
type ZapLogConfig struct {
2 years ago
LogPath string // 日志文件路径
LogName string // 日志文件名
LogLevel string // 日志级别 debug/info/warn/errordebug输出debug/info/warn/error日志。 info输出info/warn/error日志。 warn输出warn/error日志。 error输出error日志。
MaxSize int // 单个文件大小,MB
MaxBackups int // 保存的文件个数
MaxAge int // 保存的天数 0=不删除
Compress bool // 压缩
JsonFormat bool // 是否输出为json格式
ShowLine bool // 显示代码行
LogInConsole bool // 是否同时输出到控制台
}
2 years ago
type ZapLog struct {
config *ZapLogConfig
2 years ago
logger *zap.Logger
2 years ago
}
2 years ago
func NewZapLog(config *ZapLogConfig) *ZapLog {
zl := &ZapLog{config: config}
2 years ago
// 设置日志级别
var level zapcore.Level
2 years ago
switch zl.config.LogLevel {
2 years ago
case "debug":
level = zap.DebugLevel
case "info":
level = zap.InfoLevel
case "warn":
level = zap.WarnLevel
case "error":
level = zap.ErrorLevel
default:
level = zap.InfoLevel
}
var (
syncer zapcore.WriteSyncer
// 自定义时间输出格式
customTimeEncoder = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05.000"))
}
// 自定义日志级别显示
customLevelEncoder = func(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(level.CapitalString())
}
)
// 定义日志切割配置
hook := lumberjack.Logger{
2 years ago
Filename: zl.config.LogPath + zl.config.LogName, // 日志文件的位置
MaxSize: zl.config.MaxSize, // 在进行切割之前日志文件的最大大小以MB为单位
MaxBackups: zl.config.MaxBackups, // 保留旧文件的最大个数
Compress: zl.config.Compress, // 是否压缩 disabled by default
2 years ago
}
2 years ago
if zl.config.MaxAge > 0 {
hook.MaxAge = zl.config.MaxAge // days
2 years ago
}
// 判断是否控制台输出日志
2 years ago
if zl.config.LogInConsole {
2 years ago
syncer = zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(&hook))
} else {
syncer = zapcore.AddSync(&hook)
}
// 定义zap配置信息
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "line",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeTime: customTimeEncoder, // 自定义时间格式
EncodeLevel: customLevelEncoder, // 小写编码器
EncodeCaller: zapcore.ShortCallerEncoder, // 全路径编码器
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeName: zapcore.FullNameEncoder,
}
var encoder zapcore.Encoder
2 years ago
2 years ago
// 判断是否json格式输出
2 years ago
if zl.config.JsonFormat {
2 years ago
encoder = zapcore.NewJSONEncoder(encoderConfig)
} else {
encoder = zapcore.NewConsoleEncoder(encoderConfig)
}
core := zapcore.NewCore(
encoder,
syncer,
level,
)
2 years ago
zl.logger = zap.New(core)
2 years ago
// 判断是否显示代码行号
2 years ago
if zl.config.ShowLine {
2 years ago
zl.logger = zl.logger.WithOptions(zap.AddCaller())
2 years ago
}
2 years ago
return zl
2 years ago
}
2 years ago
// Panic 记录日志然后panic
func (zl *ZapLog) Panic(ctx context.Context, args ...interface{}) {
zl.logger.Sugar().Panic(args...)
}
// Panicf 记录日志然后panic
func (zl *ZapLog) Panicf(ctx context.Context, template string, args ...interface{}) {
zl.logger.Sugar().Panicf(template, args...)
}
// Fatal 有致命性错误,导致程序崩溃,记录日志,然后退出
func (zl *ZapLog) Fatal(ctx context.Context, args ...interface{}) {
zl.logger.Sugar().Fatal(args...)
}
// Fatalf 有致命性错误,导致程序崩溃,记录日志,然后退出
func (zl *ZapLog) Fatalf(ctx context.Context, template string, args ...interface{}) {
zl.logger.Sugar().Fatalf(template, args...)
}
// Error 错误日志
func (zl *ZapLog) Error(ctx context.Context, args ...interface{}) {
zl.logger.Sugar().Error(args...)
}
// Errorf 错误日志
func (zl *ZapLog) Errorf(ctx context.Context, template string, args ...interface{}) {
zl.logger.Sugar().Errorf(template, args...)
}
// Warn 警告日志
func (zl *ZapLog) Warn(ctx context.Context, args ...interface{}) {
zl.logger.Sugar().Warn(args...)
}
// Warnf 警告日志
func (zl *ZapLog) Warnf(ctx context.Context, template string, args ...interface{}) {
zl.logger.Sugar().Warnf(template, args...)
}
// Info 核心流程日志
func (zl *ZapLog) Info(ctx context.Context, args ...interface{}) {
zl.logger.Sugar().Info(args...)
}
// Infof 核心流程日志
func (zl *ZapLog) Infof(ctx context.Context, template string, args ...interface{}) {
zl.logger.Sugar().Infof(template, args...)
}
// Debug debug日志调试日志
func (zl *ZapLog) Debug(ctx context.Context, args ...interface{}) {
zl.logger.Sugar().Debug(args...)
}
// Debugf debug日志调试日志
func (zl *ZapLog) Debugf(ctx context.Context, template string, args ...interface{}) {
zl.logger.Sugar().Debugf(template, args...)
}
// Trace 粒度超细的,一般情况下我们使用不上
func (zl *ZapLog) Trace(ctx context.Context, args ...interface{}) {
zl.logger.Sugar().Debug(args...)
}