logger/file.go
2021-12-14 10:53:50 +08:00

45 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package logger
import (
"fmt"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"time"
)
// 初始化LokiCore使日志可以输出到文件
func initFileCore() zapcore.Core {
lumberJackLogger := &lumberjack.Logger{
Filename: "logs/runtime.log", // 日志文件的位置
MaxSize: 10, // 最大10M
MaxBackups: 5, // 保留旧文件的最大个数
MaxAge: 30, // 保留旧文件的最大天数
Compress: false, // 是否压缩/归档旧文件
}
// 配置 sugaredLogger
writer := zapcore.AddSync(lumberJackLogger)
// 自定义时间输出格式
customTimeEncoder := func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(fmt.Sprintf("[%v]", t.Format("2006-01-02 15:04:05.000")))
}
// 格式相关的配置
encoderConfig := zap.NewProductionEncoderConfig()
// 修改时间戳的格式
encoderConfig.EncodeTime = customTimeEncoder
// 日志级别使用大写显示
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
encoder := zapcore.NewConsoleEncoder(encoderConfig)
// 设置日志等级如果是release模式控制台只打印Error级别以上的日志
logLevel := zapcore.DebugLevel
if config.Mode == Prod {
logLevel = zapcore.ErrorLevel
}
// 设置日志级别
core := zapcore.NewCore(encoder, writer, logLevel)
return core
}