diff options
author | Wolfy-J <[email protected]> | 2020-10-28 14:38:29 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2020-10-28 14:38:29 +0300 |
commit | 39ed532b4e047ac1c0471c2b935065c89dfaecf1 (patch) | |
tree | 3f6dbd93a0d03660ef4ecfae017489c0611cc44d /plugins/logger/config.go | |
parent | d6ceb16a9a165764739137de6a2b621685bb8ac3 (diff) |
- added the ability to suppress logs
Diffstat (limited to 'plugins/logger/config.go')
-rw-r--r-- | plugins/logger/config.go | 89 |
1 files changed, 83 insertions, 6 deletions
diff --git a/plugins/logger/config.go b/plugins/logger/config.go index 4e9f1220..ba0530d2 100644 --- a/plugins/logger/config.go +++ b/plugins/logger/config.go @@ -1,14 +1,91 @@ package logger +import ( + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "strings" +) + +// ChannelConfig configures loggers per channel. +type ChannelConfig struct { + // Dedicated channels per logger. By default logger allocated via named logger. + Channels map[string]Config `json:"channels" yaml:"channels"` +} + type Config struct { - Default LoggerConfig + // Mode configures logger based on some default template (development, production, off). + Mode string `json:"mode" yaml:"mode"` + + // Level is the minimum enabled logging level. Note that this is a dynamic + // level, so calling ChannelConfig.Level.SetLevel will atomically change the log + // level of all loggers descended from this config. + Level string `json:"level" yaml:"level"` + + // Encoding sets the logger's encoding. Valid values are "json" and + // "console", as well as any third-party encodings registered via + // RegisterEncoder. + Encoding string `json:"encoding" yaml:"encoding"` - Suppress bool + // Output is a list of URLs or file paths to write logging output to. + // See Open for details. + Output []string `json:"output" yaml:"output"` - Channels map[string]LoggerConfig + // ErrorOutput is a list of URLs to write internal logger errors to. + // The default is standard error. + // + // Note that this setting only affects internal errors; for sample code that + // sends error-level logs to a different location from info- and debug-level + // logs, see the package-level AdvancedConfiguration example. + ErrorOutput []string `json:"errorOutput" yaml:"errorOutput"` } -type LoggerConfig struct { - // Level to report messages from. - Level string +// ZapConfig converts config into Zap configuration. +func (cfg *Config) BuildLogger() (*zap.Logger, error) { + var zCfg zap.Config + switch strings.ToLower(cfg.Mode) { + case "off", "none": + return zap.NewNop(), nil + case "production": + zCfg = zap.NewProductionConfig() + case "development": + zCfg = zap.NewDevelopmentConfig() + default: + zCfg = zap.Config{ + Level: zap.NewAtomicLevelAt(zap.DebugLevel), + Encoding: "console", + EncoderConfig: zapcore.EncoderConfig{ + MessageKey: "message", + LevelKey: "level", + TimeKey: "time", + NameKey: "name", + EncodeName: ColoredHashedNameEncoder, + EncodeLevel: ColoredLevelEncoder, + EncodeTime: UTCTimeEncoder, + EncodeCaller: zapcore.ShortCallerEncoder, + }, + OutputPaths: []string{"stderr"}, + ErrorOutputPaths: []string{"stderr"}, + } + } + + if cfg.Level != "" { + level := zap.NewAtomicLevel() + if err := level.UnmarshalText([]byte(cfg.Level)); err == nil { + zCfg.Level = level + } + } + + if cfg.Encoding != "" { + zCfg.Encoding = cfg.Encoding + } + + if len(cfg.Output) != 0 { + zCfg.OutputPaths = cfg.Output + } + + if len(cfg.ErrorOutput) != 0 { + zCfg.ErrorOutputPaths = cfg.ErrorOutput + } + + return zCfg.Build() } |