package levelimport ()// ErrInvalidLevelString is returned whenever an invalid string is passed to Parse.varErrInvalidLevelString = errors.New("invalid level string")// Error returns a logger that includes a Key/ErrorValue pair.func ( log.Logger) log.Logger {returnlog.WithPrefix(, Key(), ErrorValue())}// Warn returns a logger that includes a Key/WarnValue pair.func ( log.Logger) log.Logger {returnlog.WithPrefix(, Key(), WarnValue())}// Info returns a logger that includes a Key/InfoValue pair.func ( log.Logger) log.Logger {returnlog.WithPrefix(, Key(), InfoValue())}// Debug returns a logger that includes a Key/DebugValue pair.func ( log.Logger) log.Logger {returnlog.WithPrefix(, Key(), DebugValue())}// NewFilter wraps next and implements level filtering. See the commentary on// the Option functions for a detailed description of how to configure levels.// If no options are provided, all leveled log events created with Debug,// Info, Warn or Error helper methods are squelched and non-leveled log// events are passed to next unmodified.func ( log.Logger, ...Option) log.Logger { := &logger{next: , }for , := range { () }return}type logger struct { next log.Logger allowed level squelchNoLevel bool errNotAllowed error errNoLevel error}func ( *logger) ( ...interface{}) error {var , boolfor := 1; < len(); += 2 {if , := [].(*levelValue); { = true = .allowed&.level != 0break } }if ! && .squelchNoLevel {return .errNoLevel }if && ! {return .errNotAllowed }return .next.Log(...)}// Option sets a parameter for the leveled logger.typeOptionfunc(*logger)// Allow the provided log level to pass.func ( Value) Option {switch {casedebugValue:returnAllowDebug()caseinfoValue:returnAllowInfo()casewarnValue:returnAllowWarn()caseerrorValue:returnAllowError()default:returnAllowNone() }}// AllowAll is an alias for AllowDebug.func () Option {returnAllowDebug()}// AllowDebug allows error, warn, info and debug level log events to pass.func () Option {returnallowed(levelError | levelWarn | levelInfo | levelDebug)}// AllowInfo allows error, warn and info level log events to pass.func () Option {returnallowed(levelError | levelWarn | levelInfo)}// AllowWarn allows error and warn level log events to pass.func () Option {returnallowed(levelError | levelWarn)}// AllowError allows only error level log events to pass.func () Option {returnallowed(levelError)}// AllowNone allows no leveled log events to pass.func () Option {returnallowed(0)}func allowed( level) Option {returnfunc( *logger) { .allowed = }}// Parse a string to its corresponding level value. Valid strings are "debug",// "info", "warn", and "error". Strings are normalized via strings.TrimSpace and// strings.ToLower.func ( string) (Value, error) {switchstrings.TrimSpace(strings.ToLower()) {casedebugValue.name:returndebugValue, nilcaseinfoValue.name:returninfoValue, nilcasewarnValue.name:returnwarnValue, nilcaseerrorValue.name:returnerrorValue, nildefault:returnnil, ErrInvalidLevelString }}// ParseDefault calls Parse and returns the default Value on error.func ( string, Value) Value { , := Parse()if != nil {return }return}// ErrNotAllowed sets the error to return from Log when it squelches a log// event disallowed by the configured Allow[Level] option. By default,// ErrNotAllowed is nil; in this case the log event is squelched with no// error.func ( error) Option {returnfunc( *logger) { .errNotAllowed = }}// SquelchNoLevel instructs Log to squelch log events with no level, so that// they don't proceed through to the wrapped logger. If SquelchNoLevel is set// to true and a log event is squelched in this way, the error value// configured with ErrNoLevel is returned to the caller.func ( bool) Option {returnfunc( *logger) { .squelchNoLevel = }}// ErrNoLevel sets the error to return from Log when it squelches a log event// with no level. By default, ErrNoLevel is nil; in this case the log event is// squelched with no error.func ( error) Option {returnfunc( *logger) { .errNoLevel = }}// NewInjector wraps next and returns a logger that adds a Key/level pair to// the beginning of log events that don't already contain a level. In effect,// this gives a default level to logs without a level.func ( log.Logger, Value) log.Logger {return &injector{next: ,level: , }}type injector struct { next log.Logger level interface{}}func ( *injector) ( ...interface{}) error {for := 1; < len(); += 2 {if , := [].(*levelValue); {return .next.Log(...) } } := make([]interface{}, len()+2) [0], [1] = key, .levelcopy([2:], )return .next.Log(...)}// Value is the interface that each of the canonical level values implement.// It contains unexported methods that prevent types from other packages from// implementing it and guaranteeing that NewFilter can distinguish the levels// defined in this package from all other values.typeValueinterface {String() string levelVal()}// Key returns the unique key added to log events by the loggers in this// package.func () interface{} { returnkey }// ErrorValue returns the unique value added to log events by Error.func () Value { returnerrorValue }// WarnValue returns the unique value added to log events by Warn.func () Value { returnwarnValue }// InfoValue returns the unique value added to log events by Info.func () Value { returninfoValue }// DebugValue returns the unique value added to log events by Debug.func () Value { returndebugValue }var (// key is of type interface{} so that it allocates once during package // initialization and avoids allocating every time the value is added to a // []interface{} later. key interface{} = "level" errorValue = &levelValue{level: levelError, name: "error"} warnValue = &levelValue{level: levelWarn, name: "warn"} infoValue = &levelValue{level: levelInfo, name: "info"} debugValue = &levelValue{level: levelDebug, name: "debug"})type level byteconst ( levelDebug level = 1 << iota levelInfo levelWarn levelError)type levelValue struct { name stringlevel}func ( *levelValue) () string { return .name }func ( *levelValue) () {}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.