1 Star 1 Fork 0

amuluze / amutool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
encoder.go 1.72 KB
一键复制 编辑 原始数据 按行查看 历史
jialong.wang 提交于 2023-04-11 10:39 . update config and readme
// Package logx
// Date: 2023/4/10 17:23
// Author: Amu
// Description:
package logx
import (
"time"
"go.uber.org/zap/zapcore"
)
func getEncoder(config *Config) zapcore.Encoder {
if config.LogFormat == "text" {
return zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
FunctionKey: zapcore.OmitKey,
MessageKey: "message",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: cEncodeLevel,
EncodeTime: cEncodeTime,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: cEncodeCaller,
ConsoleSeparator: " || ",
})
} else {
return zapcore.NewJSONEncoder(zapcore.EncoderConfig{
// 下面以 Key 结尾的参数表示,Json格式日志中的 key
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
FunctionKey: zapcore.OmitKey,
MessageKey: "message",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format(timeFormat))
},
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
})
}
}
func cEncodeLevel(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(level.CapitalString())
}
func cEncodeTime(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format(timeFormat))
}
func cEncodeCaller(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(caller.TrimmedPath())
}
Go
1
https://gitee.com/amuluze/amutool.git
git@gitee.com:amuluze/amutool.git
amuluze
amutool
amutool
6d4b89917063

搜索帮助