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 | |
parent | d6ceb16a9a165764739137de6a2b621685bb8ac3 (diff) |
- added the ability to suppress logs
Diffstat (limited to 'plugins/logger')
-rw-r--r-- | plugins/logger/config.go | 89 | ||||
-rw-r--r-- | plugins/logger/zap_logger.go | 67 |
2 files changed, 107 insertions, 49 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() } diff --git a/plugins/logger/zap_logger.go b/plugins/logger/zap_logger.go index e1986da3..8c1739f2 100644 --- a/plugins/logger/zap_logger.go +++ b/plugins/logger/zap_logger.go @@ -4,7 +4,6 @@ import ( "github.com/spiral/endure" "github.com/spiral/roadrunner/v2/plugins/config" "go.uber.org/zap" - "go.uber.org/zap/zapcore" ) // ServiceName declares service name. @@ -20,68 +19,50 @@ type LogFactory interface { // ZapLogger manages zap logger. type ZapLogger struct { - base *zap.Logger - cfg Config + base *zap.Logger + cfg Config + channels ChannelConfig } +// Init logger service. func (z *ZapLogger) Init(cfg config.Provider) (err error) { err = cfg.UnmarshalKey(ServiceName, &z.cfg) if err != nil { return err } - if z.base == nil { - cfg := 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"}, - } - - z.base, err = cfg.Build() - if err != nil { - return err - } + err = cfg.UnmarshalKey(ServiceName, &z.channels) + if err != nil { + return err } - return nil + z.base, err = z.cfg.BuildLogger() + return err +} + +// DefaultLogger returns default logger. +func (z *ZapLogger) DefaultLogger() (*zap.Logger, error) { + return z.base, nil } -// GlobalLogger returns global log instance. -func (z *ZapLogger) GlobalLogger() *zap.Logger { - return z.base +// NamedLogger returns logger dedicated to the specific channel. Similar to Named() but also reads the core params. +func (z *ZapLogger) NamedLogger(name string) (*zap.Logger, error) { + if cfg, ok := z.channels.Channels[name]; ok { + return cfg.BuildLogger() + } + + return z.base.Named(name), nil } // NamedLogger returns logger dedicated to the specific channel. Similar to Named() but also reads the core params. -func (z *ZapLogger) NamedLogger(name string) *zap.Logger { - // todo: automatically configure - return z.base.Named(name) +func (z *ZapLogger) ServiceLogger(n endure.Named) (*zap.Logger, error) { + return z.NamedLogger(n.Name()) } // Provides declares factory methods. func (z *ZapLogger) Provides() []interface{} { return []interface{}{ z.DefaultLogger, - z.AllocateLogger, + z.ServiceLogger, } } - -// AllocateLogger allocates logger for the service. -func (z *ZapLogger) AllocateLogger(n endure.Named) (*zap.Logger, error) { - return z.NamedLogger(n.Name()), nil -} - -// DefaultLogger returns default logger. -func (z *ZapLogger) DefaultLogger() (*zap.Logger, error) { - return z.GlobalLogger(), nil -} |