2025-01-13 18:08:17 +08:00
|
|
|
|
package log
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
|
|
|
|
"os"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2025-01-14 13:09:25 +08:00
|
|
|
|
func NewLog(console, env, lv, lp string) *zap.Logger {
|
2025-01-13 18:08:17 +08:00
|
|
|
|
//目录不存在则创建
|
|
|
|
|
if _, err := os.Stat(lp); err != nil {
|
|
|
|
|
err := os.MkdirAll(lp, os.ModePerm)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-14 13:09:25 +08:00
|
|
|
|
|
2025-01-13 18:08:17 +08:00
|
|
|
|
var level zapcore.Level
|
|
|
|
|
//debug<info<warn<error<fatal<panic
|
|
|
|
|
switch lv {
|
|
|
|
|
case "debug":
|
|
|
|
|
level = zap.DebugLevel
|
|
|
|
|
case "info":
|
|
|
|
|
level = zap.InfoLevel
|
|
|
|
|
case "warn":
|
|
|
|
|
level = zap.WarnLevel
|
|
|
|
|
case "error":
|
|
|
|
|
level = zap.ErrorLevel
|
|
|
|
|
default:
|
|
|
|
|
level = zap.InfoLevel
|
|
|
|
|
}
|
|
|
|
|
//创建了一个 lumberjack.Logger 实例,用于配置日志文件的存储。
|
|
|
|
|
hook := lumberjack.Logger{
|
|
|
|
|
Filename: lp, // Log file path 日志文件的路径
|
|
|
|
|
MaxSize: 1024, // Maximum size unit for each log file: M 每个日志最大大小
|
|
|
|
|
MaxBackups: 30, // The maximum number of backups that can be saved for log files 可以保存的日志文件数量
|
|
|
|
|
MaxAge: 7, // Maximum number of days the file can be saved 日志文件保存的最大天数
|
|
|
|
|
Compress: true, // Compression or not 是否对日志文件进行压缩
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//encoder:这是一个 zapcore.Encoder 实例,用于定义日志的格式。
|
|
|
|
|
//定义一个打印格式 json还是console
|
|
|
|
|
//console := "console"
|
|
|
|
|
var encoder zapcore.Encoder
|
|
|
|
|
if console == "console" {
|
|
|
|
|
encoder = zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
|
|
|
|
|
TimeKey: "ts",
|
|
|
|
|
LevelKey: "level",
|
|
|
|
|
NameKey: "Logger",
|
|
|
|
|
CallerKey: "caller",
|
|
|
|
|
MessageKey: "msg",
|
|
|
|
|
StacktraceKey: "stacktrace",
|
|
|
|
|
LineEnding: zapcore.DefaultLineEnding,
|
|
|
|
|
EncodeLevel: zapcore.LowercaseColorLevelEncoder,
|
|
|
|
|
EncodeTime: timeEncoder,
|
|
|
|
|
EncodeDuration: zapcore.SecondsDurationEncoder,
|
|
|
|
|
EncodeCaller: zapcore.FullCallerEncoder,
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
encoder = zapcore.NewJSONEncoder(zapcore.EncoderConfig{
|
|
|
|
|
TimeKey: "ts",
|
|
|
|
|
LevelKey: "level",
|
|
|
|
|
NameKey: "logger",
|
|
|
|
|
CallerKey: "caller",
|
|
|
|
|
FunctionKey: zapcore.OmitKey,
|
|
|
|
|
MessageKey: "msg",
|
|
|
|
|
StacktraceKey: "stacktrace",
|
|
|
|
|
LineEnding: zapcore.DefaultLineEnding,
|
|
|
|
|
EncodeLevel: zapcore.LowercaseLevelEncoder,
|
|
|
|
|
EncodeTime: zapcore.EpochTimeEncoder,
|
|
|
|
|
EncodeDuration: zapcore.SecondsDurationEncoder,
|
|
|
|
|
EncodeCaller: zapcore.ShortCallerEncoder,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
core := zapcore.NewCore(
|
|
|
|
|
encoder,
|
|
|
|
|
//通过(NewMultiWriteSyncer)它将日志输出到多个目标。
|
|
|
|
|
//zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(&hook)), // 同时讲日志输出到控制台和文件
|
|
|
|
|
zapcore.NewMultiWriteSyncer(zapcore.AddSync(&hook)), // Print to file
|
|
|
|
|
level,
|
|
|
|
|
)
|
|
|
|
|
//定义一个环境生产环境还是开发环境
|
|
|
|
|
//env := "local"
|
2025-01-14 13:09:25 +08:00
|
|
|
|
var log *zap.Logger
|
|
|
|
|
|
2025-01-13 18:08:17 +08:00
|
|
|
|
if env != "prod" {
|
|
|
|
|
// 开发模式下 Zap 会记录更详细的日志信息,包括调用者信息和错误级别的堆栈跟踪
|
2025-01-14 13:09:25 +08:00
|
|
|
|
log = zap.New(core, zap.Development(), zap.AddCaller(), zap.AddStacktrace(zap.ErrorLevel))
|
|
|
|
|
} else {
|
|
|
|
|
//如果是生产环境,创建一个 Logger 实例,仅记录调用者信息和错误级别的堆栈跟踪
|
|
|
|
|
log = zap.New(core, zap.AddCaller(), zap.AddStacktrace(zap.ErrorLevel))
|
2025-01-13 18:08:17 +08:00
|
|
|
|
}
|
2025-01-14 13:09:25 +08:00
|
|
|
|
zap.ReplaceGlobals(log)
|
|
|
|
|
|
|
|
|
|
return log
|
2025-01-13 18:08:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// timeEncoder 格式化一下当前实际
|
|
|
|
|
func timeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
|
|
|
|
//enc.AppendString(t.Format("2006-01-02 15:04:05"))
|
|
|
|
|
enc.AppendString(t.Format("2006-01-02 15:04:05.000000000"))
|
|
|
|
|
}
|