2023-11-24 15:23:07 +03:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
2024-04-10 14:06:41 +03:00
|
|
|
"fmt"
|
2023-11-24 15:23:07 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
)
|
|
|
|
|
2024-04-10 14:06:41 +03:00
|
|
|
func NewZap(format string, debug bool) *zap.Logger {
|
|
|
|
switch format {
|
|
|
|
case "json":
|
|
|
|
return NewZapJSON(debug)
|
|
|
|
case "console":
|
|
|
|
return NewZapConsole(debug)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown logger format: %s", format))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewZapConsole(debug bool) *zap.Logger {
|
2023-11-24 15:23:07 +03:00
|
|
|
level := zapcore.InfoLevel
|
|
|
|
if debug {
|
|
|
|
level = zapcore.DebugLevel
|
|
|
|
}
|
|
|
|
log, err := zap.Config{
|
|
|
|
Level: zap.NewAtomicLevelAt(level),
|
|
|
|
Development: debug,
|
|
|
|
Encoding: "console",
|
2024-04-10 14:06:41 +03:00
|
|
|
EncoderConfig: EncoderConfigConsole(),
|
2023-11-24 15:23:07 +03:00
|
|
|
OutputPaths: []string{"stdout"},
|
|
|
|
ErrorOutputPaths: []string{"stderr"},
|
|
|
|
}.Build()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return log
|
|
|
|
}
|
|
|
|
|
2024-04-10 14:06:41 +03:00
|
|
|
func EncoderConfigConsole() zapcore.EncoderConfig {
|
2023-11-24 15:23:07 +03:00
|
|
|
return zapcore.EncoderConfig{
|
|
|
|
MessageKey: "message",
|
|
|
|
LevelKey: "level",
|
2024-04-17 11:42:31 +03:00
|
|
|
TimeKey: "datetime",
|
2023-11-24 15:23:07 +03:00
|
|
|
NameKey: "logger",
|
2024-09-19 17:09:58 +03:00
|
|
|
CallerKey: "",
|
2023-11-24 15:23:07 +03:00
|
|
|
FunctionKey: zapcore.OmitKey,
|
|
|
|
StacktraceKey: "",
|
|
|
|
LineEnding: "\n",
|
|
|
|
EncodeLevel: func(level zapcore.Level, encoder zapcore.PrimitiveArrayEncoder) {
|
2024-04-17 11:42:31 +03:00
|
|
|
encoder.AppendString("level_name=" + level.CapitalString())
|
2023-11-24 15:23:07 +03:00
|
|
|
},
|
|
|
|
EncodeTime: func(t time.Time, encoder zapcore.PrimitiveArrayEncoder) {
|
2024-04-17 11:42:31 +03:00
|
|
|
encoder.AppendString("datetime=" + t.Format(time.RFC3339))
|
2023-11-24 15:23:07 +03:00
|
|
|
},
|
|
|
|
EncodeDuration: zapcore.StringDurationEncoder,
|
|
|
|
EncodeCaller: func(caller zapcore.EntryCaller, encoder zapcore.PrimitiveArrayEncoder) {
|
|
|
|
encoder.AppendString("caller=" + caller.TrimmedPath())
|
|
|
|
},
|
|
|
|
EncodeName: zapcore.FullNameEncoder,
|
|
|
|
ConsoleSeparator: " ",
|
|
|
|
}
|
|
|
|
}
|
2024-04-10 14:06:41 +03:00
|
|
|
|
|
|
|
func NewZapJSON(debug bool) *zap.Logger {
|
|
|
|
level := zapcore.InfoLevel
|
|
|
|
if debug {
|
|
|
|
level = zapcore.DebugLevel
|
|
|
|
}
|
|
|
|
log, err := zap.Config{
|
|
|
|
Level: zap.NewAtomicLevelAt(level),
|
|
|
|
Development: debug,
|
2024-06-07 17:55:45 +03:00
|
|
|
Encoding: "json-with-context",
|
2024-04-10 14:06:41 +03:00
|
|
|
EncoderConfig: EncoderConfigJSON(),
|
|
|
|
OutputPaths: []string{"stdout"},
|
|
|
|
ErrorOutputPaths: []string{"stderr"},
|
|
|
|
}.Build()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return log
|
|
|
|
}
|
|
|
|
|
|
|
|
func EncoderConfigJSON() zapcore.EncoderConfig {
|
|
|
|
return zapcore.EncoderConfig{
|
|
|
|
MessageKey: "message",
|
2024-04-17 11:42:31 +03:00
|
|
|
LevelKey: "level_name",
|
|
|
|
TimeKey: "datetime",
|
2024-04-10 14:06:41 +03:00
|
|
|
NameKey: "logger",
|
2024-09-19 17:09:58 +03:00
|
|
|
CallerKey: "",
|
2024-04-10 14:06:41 +03:00
|
|
|
FunctionKey: zapcore.OmitKey,
|
|
|
|
StacktraceKey: "",
|
|
|
|
LineEnding: "\n",
|
|
|
|
EncodeLevel: zapcore.CapitalLevelEncoder,
|
|
|
|
EncodeTime: zapcore.RFC3339TimeEncoder,
|
|
|
|
EncodeDuration: zapcore.StringDurationEncoder,
|
|
|
|
EncodeCaller: zapcore.ShortCallerEncoder,
|
|
|
|
EncodeName: zapcore.FullNameEncoder,
|
|
|
|
ConsoleSeparator: " ",
|
|
|
|
}
|
|
|
|
}
|