Package list golang-github-go-kit-kit / 5dc7902
log: More concise functions names for experimental_level package. Chris Hines 4 years ago
4 changed file(s) with 26 addition(s) and 25 deletion(s). Raw diff Collapse all Expand all
4343 return l
4444 }},
4545 {"DisallowedLevel", func(l log.Logger) log.Logger {
46 return level.NewFilter(l, level.AllowInfoAndAbove())
46 return level.NewFilter(l, level.AllowInfo())
4747 }},
4848 {"AllowedLevel", func(l log.Logger) log.Logger {
4949 return level.NewFilter(l, level.AllowAll())
1010 func Example_basic() {
1111 // setup logger with level filter
1212 logger := log.NewLogfmtLogger(os.Stdout)
13 logger = level.NewFilter(logger, level.AllowInfoAndAbove())
13 logger = level.NewFilter(logger, level.AllowInfo())
1414 logger = log.NewContext(logger).With("caller", log.DefaultCaller)
1515
1616 // use level helpers to log at different levels
6565 // Option sets a parameter for the leveled logger.
6666 type Option func(*logger)
6767
68 // AllowAll is an alias for AllowDebugAndAbove.
68 // AllowAll is an alias for AllowDebug.
6969 func AllowAll() Option {
70 return AllowDebugAndAbove()
70 return AllowDebug()
7171 }
7272
73 // AllowDebugAndAbove allows all of the four default log levels.
74 func AllowDebugAndAbove() Option {
75 return allowed(levelDebug | levelInfo | levelWarn | levelError)
73 // AllowDebug allows error, warn, info and debug level log events to pass.
74 func AllowDebug() Option {
75 return allowed(levelError | levelWarn | levelInfo | levelDebug)
7676 }
7777
78 // AllowInfoAndAbove allows the default info, warn, and error log levels.
79 func AllowInfoAndAbove() Option {
80 return allowed(levelInfo | levelWarn | levelError)
78 // AllowInfo allows error, warn and info level log events to pass.
79 func AllowInfo() Option {
80 return allowed(levelError | levelWarn | levelInfo)
8181 }
8282
83 // AllowWarnAndAbove allows the default warn and error log levels.
84 func AllowWarnAndAbove() Option {
85 return allowed(levelWarn | levelError)
83 // AllowWarn allows error and warn level log events to pass.
84 func AllowWarn() Option {
85 return allowed(levelError | levelWarn)
8686 }
8787
88 // AllowErrorOnly allows only the default error log level.
89 func AllowErrorOnly() Option {
88 // AllowError allows only error level log events to pass.
89 func AllowError() Option {
9090 return allowed(levelError)
9191 }
9292
93 // AllowNone allows none of the default log levels.
93 // AllowNone allows no leveled log events to pass.
9494 func AllowNone() Option {
9595 return allowed(0)
9696 }
100100 }
101101
102102 // ErrNotAllowed sets the error to return from Log when it squelches a log
103 // event below the configured filtering level. By default, ErrNotAllowed is
104 // nil; in this case, the log event is squelched with no error.
103 // event disallowed by the configured Allow[Level] option. By default,
104 // ErrNotAllowed is nil; in this case the log event is squelched with no
105 // error.
105106 func ErrNotAllowed(err error) Option {
106107 return func(l *logger) { l.errNotAllowed = err }
107108 }
115116 }
116117
117118 // ErrNoLevel sets the error to return from Log when it squelches a log event
118 // with no level. By default, ErrNoLevel is nil; in this case, the log event
119 // is squelched with no error.
119 // with no level. By default, ErrNoLevel is nil; in this case the log event is
120 // squelched with no error.
120121 func ErrNoLevel(err error) Option {
121122 return func(l *logger) { l.errNoLevel = err }
122123 }
2525 }, "\n"),
2626 },
2727 {
28 level.AllowDebugAndAbove(),
28 level.AllowDebug(),
2929 strings.Join([]string{
3030 `{"level":"debug","this is":"debug log"}`,
3131 `{"level":"info","this is":"info log"}`,
3434 }, "\n"),
3535 },
3636 {
37 level.AllowInfoAndAbove(),
37 level.AllowInfo(),
3838 strings.Join([]string{
3939 `{"level":"info","this is":"info log"}`,
4040 `{"level":"warn","this is":"warn log"}`,
4242 }, "\n"),
4343 },
4444 {
45 level.AllowWarnAndAbove(),
45 level.AllowWarn(),
4646 strings.Join([]string{
4747 `{"level":"warn","this is":"warn log"}`,
4848 `{"level":"error","this is":"error log"}`,
4949 }, "\n"),
5050 },
5151 {
52 level.AllowErrorOnly(),
52 level.AllowError(),
5353 strings.Join([]string{
5454 `{"level":"error","this is":"error log"}`,
5555 }, "\n"),
7676 func TestErrNotAllowed(t *testing.T) {
7777 myError := errors.New("squelched!")
7878 opts := []level.Option{
79 level.AllowWarnAndAbove(),
79 level.AllowWarn(),
8080 level.ErrNotAllowed(myError),
8181 }
8282 logger := level.NewFilter(log.NewNopLogger(), opts...)