mirror of
https://github.com/retailcrm/mg-transport-core.git
synced 2024-11-22 05:06:04 +03:00
53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package logger
|
|
|
|
import (
|
|
"time"
|
|
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap/zapcore"
|
|
)
|
|
|
|
func NewZap(debug bool) *zap.Logger {
|
|
level := zapcore.InfoLevel
|
|
if debug {
|
|
level = zapcore.DebugLevel
|
|
}
|
|
log, err := zap.Config{
|
|
Level: zap.NewAtomicLevelAt(level),
|
|
Development: debug,
|
|
Encoding: "console",
|
|
EncoderConfig: EncoderConfig(),
|
|
OutputPaths: []string{"stdout"},
|
|
ErrorOutputPaths: []string{"stderr"},
|
|
}.Build()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return log
|
|
}
|
|
|
|
func EncoderConfig() zapcore.EncoderConfig {
|
|
return zapcore.EncoderConfig{
|
|
MessageKey: "message",
|
|
LevelKey: "level",
|
|
TimeKey: "timestamp",
|
|
NameKey: "logger",
|
|
CallerKey: "caller",
|
|
FunctionKey: zapcore.OmitKey,
|
|
StacktraceKey: "",
|
|
LineEnding: "\n",
|
|
EncodeLevel: func(level zapcore.Level, encoder zapcore.PrimitiveArrayEncoder) {
|
|
encoder.AppendString("level=" + level.String())
|
|
},
|
|
EncodeTime: func(t time.Time, encoder zapcore.PrimitiveArrayEncoder) {
|
|
encoder.AppendString("time=" + t.Format(time.RFC3339))
|
|
},
|
|
EncodeDuration: zapcore.StringDurationEncoder,
|
|
EncodeCaller: func(caller zapcore.EntryCaller, encoder zapcore.PrimitiveArrayEncoder) {
|
|
encoder.AppendString("caller=" + caller.TrimmedPath())
|
|
},
|
|
EncodeName: zapcore.FullNameEncoder,
|
|
ConsoleSeparator: " ",
|
|
}
|
|
}
|