Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DebugLogger = func(lvl LogLevel, formatString string, a ...interface{}) { msg := fmt.Sprintf(formatString, a...) fmt.Printf(formatString, lvl, msg) }
DebugLogger logs all messages of level debug or above
var ErrorLogger = func(lvl LogLevel, formatString string, a ...interface{}) { if lvl != LogLevelError { return } msg := fmt.Sprintf(formatString, a...) fmt.Printf(formatString, lvl, msg) }
ErrorLogger logs all messages of level error
var InfoLogger = func(lvl LogLevel, formatString string, a ...interface{}) { if lvl == LogLevelDebug { return } msg := fmt.Sprintf(formatString, a...) fmt.Printf(formatString, lvl, msg) }
InfoLogger logs all messages of level info or above
var NoLogging = func(lvl LogLevel, formatString string, a ...interface{}) {
}
NoLogging logs no messages at all
var WarnLogger = func(lvl LogLevel, formatString string, a ...interface{}) { if lvl == LogLevelDebug || lvl == LogLevelInfo { return } msg := fmt.Sprintf(formatString, a...) fmt.Printf(formatString, lvl, msg) }
WarnLogger logs all messages of level warn or above
Functions ¶
This section is empty.
Types ¶
type LogLevel ¶
type LogLevel string
LogLevel is a type to specify the log level
const ( // LogLevelDebug debug loglevel (contains messages of type debug, info, warn and error) LogLevelDebug LogLevel = "Debug" // LogLevelInfo info loglevel (contains messages of type info, warn and error) LogLevelInfo LogLevel = "Info" // LogLevelWarn waring loglevel (contains messages of type warn and error) LogLevelWarn LogLevel = "Warn" // LogLevelError error loglevel (contains messages of type error) LogLevelError LogLevel = "Error" )
type LoggerFunc ¶
LoggerFunc is a function type that is called whenever the library wants to log a message. This type can be implemented to integrate your custom logging system.
type MappingFunc ¶
MappingFunc type that specifies a mapping function.
rawUntypedValue - the incoming value
targetType - the type the returned result should have
type Provider ¶
type Provider interface { /* RegisterMappingFunc used to register a function that will map the value and type provided as command line parameter or default value. This handles the case where the type of a field defined in the config annotation does not match the type of the field that is annotated. Example: type cfg1 struct { LogLevel zerolog.Level `cfg:"{'name':'logl','default':'info','mapfun':'strToLogLevel'}"` } // [..] provider.RegisterMappingFunc("toUpperCase",func(rawUntypedValue interface{}, targetType reflect.Type) (interface{}, error){ asStr, ok := rawUntypedValue.(string) if !ok { return nil, fmt.Errorf("Expected a string. Type '%T' is not supported", rawUntypedValue) } return zerolog.ParseLevel(asStr) }) Here F1 is of type zerolog.Level (int8) and the defined type in the annotation is string (based on the default value). In order to support this situation we have to apply the defined mapping functions. A mapping function can be used also to do some conversions. For example if string value just should be converted to upper case. Example: type cfg2 struct { LogLevel string `cfg:"{'name':'logl','default':'info','mapfun':'toUpperCase'}"` } // [..] provider.RegisterMappingFunc("toUpperCase",func(rawUntypedValue interface{}, targetType reflect.Type) (interface{}, error){ asStr,ok := rawUntypedValue.(string) if !ok { return nil, fmt.Errorf("Value must be a string") } return strings.ToUpper(asStr),nil }) */ RegisterMappingFunc(name string, mFunc MappingFunc) error ReadConfig(args []string) error Get(key string) interface{} GetString(key string) string GetBool(key string) bool GetInt(key string) int GetInt32(key string) int32 GetInt64(key string) int64 GetUint(key string) uint GetUint32(key string) uint32 GetUint64(key string) uint64 GetFloat64(key string) float64 GetTime(key string) time.Time GetDuration(key string) time.Duration GetIntSlice(key string) []int GetStringSlice(key string) []string GetStringMap(key string) map[string]interface{} GetStringMapString(key string) map[string]string GetStringMapStringSlice(key string) map[string][]string GetSizeInBytes(key string) uint IsSet(key string) bool String() string Log(lvl LogLevel, formatString string, a ...interface{}) Usage() string }
Provider consists of functions needed to interact with a configuration such as reading the config, register mapping functions and obtaining read values.