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

38 lines
1018 B
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"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"time"
)
// 初始化打印到控制台的ZapCore
func initConsoleCore() zapcore.Core {
// 配置 sugaredLogger
writer := zapcore.AddSync(os.Stdout)
// 自定义时间输出格式
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.CapitalColorLevelEncoder
encoder := zapcore.NewConsoleEncoder(encoderConfig)
// 设置日志等级如果是release模式控制台只打印Error级别以上的日志
logLevel := zapcore.DebugLevel
if config.Mode == Prod {
logLevel = zapcore.ErrorLevel
}
// 设置日志级别
core := zapcore.NewCore(encoder, writer, logLevel)
return core
}