github.com/lyft/flytestdlib@v0.3.12-0.20210213045714-8cdd111ecda1/logger/config.go (about) 1 package logger 2 3 import ( 4 "context" 5 6 "github.com/lyft/flytestdlib/config" 7 ) 8 9 //go:generate pflags Config --default-var defaultConfig 10 11 const configSectionKey = "Logger" 12 13 type FormatterType = string 14 15 const ( 16 FormatterJSON FormatterType = "json" 17 FormatterText FormatterType = "text" 18 ) 19 20 const ( 21 jsonDataKey string = "json" 22 ) 23 24 var ( 25 defaultConfig = &Config{ 26 Formatter: FormatterConfig{ 27 Type: FormatterJSON, 28 }, 29 Level: InfoLevel, 30 } 31 32 configSection = config.MustRegisterSectionWithUpdates(configSectionKey, defaultConfig, func(ctx context.Context, newValue config.Config) { 33 onConfigUpdated(*newValue.(*Config)) 34 }) 35 ) 36 37 // Global logger config. 38 type Config struct { 39 // Determines whether to include source code location in logs. This might incurs a performance hit and is only 40 // recommended on debug/development builds. 41 IncludeSourceCode bool `json:"show-source" pflag:",Includes source code location in logs."` 42 43 // Determines whether the logger should mute all logs (including panics) 44 Mute bool `json:"mute" pflag:",Mutes all logs regardless of severity. Intended for benchmarks/tests only."` 45 46 // Determines the minimum log level to log. 47 Level Level `json:"level" pflag:",Sets the minimum logging level."` 48 49 Formatter FormatterConfig `json:"formatter" pflag:",Sets logging format."` 50 } 51 52 type FormatterConfig struct { 53 Type FormatterType `json:"type" pflag:",Sets logging format type."` 54 } 55 56 // Sets global logger config 57 func SetConfig(cfg *Config) error { 58 if err := configSection.SetConfig(cfg); err != nil { 59 return err 60 } 61 62 onConfigUpdated(*cfg) 63 return nil 64 } 65 66 func GetConfig() *Config { 67 return configSection.GetConfig().(*Config) 68 } 69 70 // Level type. 71 type Level = int 72 73 // These are the different logging levels. 74 const ( 75 // PanicLevel level, highest level of severity. Logs and then calls panic with the 76 // message passed to Debug, Info, ... 77 PanicLevel Level = iota 78 // FatalLevel level. Logs and then calls `os.Exit(1)`. It will exit even if the 79 // logging level is set to Panic. 80 FatalLevel 81 // ErrorLevel level. Logs. Used for errors that should definitely be noted. 82 // Commonly used for hooks to send errors to an error tracking service. 83 ErrorLevel 84 // WarnLevel level. Non-critical entries that deserve eyes. 85 WarnLevel 86 // InfoLevel level. General operational entries about what's going on inside the 87 // application. 88 InfoLevel 89 // DebugLevel level. Usually only enabled when debugging. Very verbose logging. 90 DebugLevel 91 )