По мотивам https://github.com/sandipb/zap-examples/tree/master/src/customlogger
Created
June 16, 2021 22:05
-
-
Save f4rx/53a4a40c73ba088ee93bdb3df30089c8 to your computer and use it in GitHub Desktop.
zap-logger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"go.uber.org/zap" | |
"go.uber.org/zap/zapcore" | |
) | |
func main() { | |
encoding := "console" | |
// encoding := "json" | |
encodeLevel := zapcore.CapitalColorLevelEncoder | |
if encoding == "json" { | |
encodeLevel = zapcore.CapitalLevelEncoder | |
} | |
level := zapcore.DebugLevel | |
// level := zapcore.InfoLevel | |
cfg := zap.Config{ | |
Encoding: encoding, | |
Level: zap.NewAtomicLevelAt(level), | |
OutputPaths: []string{"stdout"}, | |
ErrorOutputPaths: []string{"stdout"}, | |
EncoderConfig: zapcore.EncoderConfig{ | |
TimeKey: "time", | |
LevelKey: "level", | |
NameKey: "logger", | |
CallerKey: "caller", | |
FunctionKey: zapcore.OmitKey, | |
MessageKey: "msg", | |
StacktraceKey: "stacktrace", | |
LineEnding: zapcore.DefaultLineEnding, | |
EncodeLevel: encodeLevel, | |
EncodeTime: zapcore.ISO8601TimeEncoder, | |
EncodeDuration: zapcore.SecondsDurationEncoder, | |
EncodeCaller: zapcore.ShortCallerEncoder, | |
}, | |
} | |
logger, _ := cfg.Build() | |
defer logger.Sync() | |
logger.Debug("This is a DEBUG message") | |
logger.Info("This is an INFO message") | |
logger.Info("This is an INFO message with fields", zap.String("region", "us-west"), zap.Int("id", 2)) | |
// logger.Panic("Panic") | |
logger.Info("This is an INFO message") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment