0 | 0 |
package level
|
1 | 1 |
|
2 | |
import (
|
3 | |
"github.com/go-kit/kit/log"
|
4 | |
)
|
|
2 |
import "github.com/go-kit/kit/log"
|
5 | 3 |
|
6 | |
var (
|
7 | |
levelKey = "level"
|
8 | |
errorLevelValue = "error"
|
9 | |
warnLevelValue = "warn"
|
10 | |
infoLevelValue = "info"
|
11 | |
debugLevelValue = "debug"
|
12 | |
)
|
13 | |
|
14 | |
// AllowAll is an alias for AllowDebugAndAbove.
|
15 | |
func AllowAll() []string {
|
16 | |
return AllowDebugAndAbove()
|
17 | |
}
|
18 | |
|
19 | |
// AllowDebugAndAbove allows all of the four default log levels.
|
20 | |
// Its return value may be provided with the Allowed Option.
|
21 | |
func AllowDebugAndAbove() []string {
|
22 | |
return []string{errorLevelValue, warnLevelValue, infoLevelValue, debugLevelValue}
|
23 | |
}
|
24 | |
|
25 | |
// AllowInfoAndAbove allows the default info, warn, and error log levels.
|
26 | |
// Its return value may be provided with the Allowed Option.
|
27 | |
func AllowInfoAndAbove() []string {
|
28 | |
return []string{errorLevelValue, warnLevelValue, infoLevelValue}
|
29 | |
}
|
30 | |
|
31 | |
// AllowWarnAndAbove allows the default warn and error log levels.
|
32 | |
// Its return value may be provided with the Allowed Option.
|
33 | |
func AllowWarnAndAbove() []string {
|
34 | |
return []string{errorLevelValue, warnLevelValue}
|
35 | |
}
|
36 | |
|
37 | |
// AllowErrorOnly allows only the default error log level.
|
38 | |
// Its return value may be provided with the Allowed Option.
|
39 | |
func AllowErrorOnly() []string {
|
40 | |
return []string{errorLevelValue}
|
41 | |
}
|
42 | |
|
43 | |
// AllowNone allows none of the default log levels.
|
44 | |
// Its return value may be provided with the Allowed Option.
|
45 | |
func AllowNone() []string {
|
46 | |
return []string{}
|
47 | |
}
|
48 | |
|
49 | |
// Error returns a logger with the level key set to ErrorLevelValue.
|
|
4 |
// Error returns a logger that includes an error level keyval.
|
50 | 5 |
func Error(logger log.Logger) log.Logger {
|
51 | 6 |
return log.NewContext(logger).WithPrefix(levelKey, errorLevelValue)
|
52 | 7 |
}
|
53 | 8 |
|
54 | |
// Warn returns a logger with the level key set to WarnLevelValue.
|
|
9 |
// Warn returns a logger that includes a warn level keyval.
|
55 | 10 |
func Warn(logger log.Logger) log.Logger {
|
56 | 11 |
return log.NewContext(logger).WithPrefix(levelKey, warnLevelValue)
|
57 | 12 |
}
|
58 | 13 |
|
59 | |
// Info returns a logger with the level key set to InfoLevelValue.
|
|
14 |
// Info returns a logger that includes an info level keyval.
|
60 | 15 |
func Info(logger log.Logger) log.Logger {
|
61 | 16 |
return log.NewContext(logger).WithPrefix(levelKey, infoLevelValue)
|
62 | 17 |
}
|
63 | 18 |
|
64 | |
// Debug returns a logger with the level key set to DebugLevelValue.
|
|
19 |
// Debug returns a logger that includes a debug level keyval.
|
65 | 20 |
func Debug(logger log.Logger) log.Logger {
|
66 | 21 |
return log.NewContext(logger).WithPrefix(levelKey, debugLevelValue)
|
67 | 22 |
}
|
68 | 23 |
|
69 | |
// New wraps the logger and implements level checking. See the commentary on the
|
70 | |
// Option functions for a detailed description of how to configure levels.
|
71 | |
// If no options are provided, all leveled log events created with level.Debug,
|
72 | |
// Info, Warn or Error helper methods will be squelched.
|
73 | |
func New(next log.Logger, options ...Option) log.Logger {
|
74 | |
l := logger{
|
|
24 |
// NewFilter wraps next and implements level filtering. See the commentary on
|
|
25 |
// the Option functions for a detailed description of how to configure levels.
|
|
26 |
// If no options are provided, all leveled log events created with Debug,
|
|
27 |
// Info, Warn or Error helper methods are squelched and non-leveled log
|
|
28 |
// events are passed to next unmodified.
|
|
29 |
func NewFilter(next log.Logger, options ...Option) log.Logger {
|
|
30 |
l := &logger{
|
75 | 31 |
next: next,
|
76 | 32 |
}
|
77 | 33 |
for _, option := range options {
|
78 | |
option(&l)
|
|
34 |
option(l)
|
79 | 35 |
}
|
80 | |
return &l
|
|
36 |
return l
|
81 | 37 |
}
|
82 | |
|
83 | |
// Allowed enumerates the accepted log levels. If a log event is encountered
|
84 | |
// with a level key set to a value that isn't explicitly allowed, the event
|
85 | |
// will be squelched, and ErrNotAllowed returned.
|
86 | |
func Allowed(allowed []string) Option {
|
87 | |
return func(l *logger) { l.allowed = makeSet(allowed) }
|
88 | |
}
|
89 | |
|
90 | |
// ErrNoLevel is returned to the caller when SquelchNoLevel is true, and Log
|
91 | |
// is invoked without a level key. By default, ErrNoLevel is nil; in this
|
92 | |
// case, the log event is squelched with no error.
|
93 | |
func ErrNotAllowed(err error) Option {
|
94 | |
return func(l *logger) { l.errNotAllowed = err }
|
95 | |
}
|
96 | |
|
97 | |
// SquelchNoLevel will squelch log events with no level key, so that they
|
98 | |
// don't proceed through to the wrapped logger. If SquelchNoLevel is set to
|
99 | |
// true and a log event is squelched in this way, ErrNoLevel is returned to
|
100 | |
// the caller.
|
101 | |
func SquelchNoLevel(squelch bool) Option {
|
102 | |
return func(l *logger) { l.squelchNoLevel = squelch }
|
103 | |
}
|
104 | |
|
105 | |
// ErrNoLevel is returned to the caller when SquelchNoLevel is true, and Log
|
106 | |
// is invoked without a level key. By default, ErrNoLevel is nil; in this
|
107 | |
// case, the log event is squelched with no error.
|
108 | |
func ErrNoLevel(err error) Option {
|
109 | |
return func(l *logger) { l.errNoLevel = err }
|
110 | |
}
|
111 | |
|
112 | |
// Option sets a parameter for the leveled logger.
|
113 | |
type Option func(*logger)
|
114 | 38 |
|
115 | 39 |
type logger struct {
|
116 | 40 |
next log.Logger
|
117 | |
allowed map[string]struct{}
|
|
41 |
allowed level
|
|
42 |
squelchNoLevel bool
|
118 | 43 |
errNotAllowed error
|
119 | |
squelchNoLevel bool
|
120 | 44 |
errNoLevel error
|
121 | 45 |
}
|
122 | 46 |
|
123 | 47 |
func (l *logger) Log(keyvals ...interface{}) error {
|
124 | 48 |
var hasLevel, levelAllowed bool
|
125 | |
for i := 0; i < len(keyvals); i += 2 {
|
126 | |
if k, ok := keyvals[i].(string); !ok || k != levelKey {
|
127 | |
continue
|
|
49 |
for i := 1; i < len(keyvals); i += 2 {
|
|
50 |
if v, ok := keyvals[i].(*levelValue); ok {
|
|
51 |
hasLevel = true
|
|
52 |
levelAllowed = l.allowed&v.level != 0
|
|
53 |
break
|
128 | 54 |
}
|
129 | |
hasLevel = true
|
130 | |
if i >= len(keyvals) {
|
131 | |
continue
|
132 | |
}
|
133 | |
v, ok := keyvals[i+1].(string)
|
134 | |
if !ok {
|
135 | |
continue
|
136 | |
}
|
137 | |
_, levelAllowed = l.allowed[v]
|
138 | |
break
|
139 | 55 |
}
|
140 | 56 |
if !hasLevel && l.squelchNoLevel {
|
141 | 57 |
return l.errNoLevel
|
|
146 | 62 |
return l.next.Log(keyvals...)
|
147 | 63 |
}
|
148 | 64 |
|
149 | |
func makeSet(a []string) map[string]struct{} {
|
150 | |
m := make(map[string]struct{}, len(a))
|
151 | |
for _, s := range a {
|
152 | |
m[s] = struct{}{}
|
153 | |
}
|
154 | |
return m
|
|
65 |
// Option sets a parameter for the leveled logger.
|
|
66 |
type Option func(*logger)
|
|
67 |
|
|
68 |
// AllowAll is an alias for AllowDebugAndAbove.
|
|
69 |
func AllowAll() Option {
|
|
70 |
return AllowDebugAndAbove()
|
155 | 71 |
}
|
|
72 |
|
|
73 |
// AllowDebugAndAbove allows all of the four default log levels.
|
|
74 |
func AllowDebugAndAbove() Option {
|
|
75 |
return allowed(levelDebug | levelInfo | levelWarn | levelError)
|
|
76 |
}
|
|
77 |
|
|
78 |
// AllowInfoAndAbove allows the default info, warn, and error log levels.
|
|
79 |
func AllowInfoAndAbove() Option {
|
|
80 |
return allowed(levelInfo | levelWarn | levelError)
|
|
81 |
}
|
|
82 |
|
|
83 |
// AllowWarnAndAbove allows the default warn and error log levels.
|
|
84 |
func AllowWarnAndAbove() Option {
|
|
85 |
return allowed(levelWarn | levelError)
|
|
86 |
}
|
|
87 |
|
|
88 |
// AllowErrorOnly allows only the default error log level.
|
|
89 |
func AllowErrorOnly() Option {
|
|
90 |
return allowed(levelError)
|
|
91 |
}
|
|
92 |
|
|
93 |
// AllowNone allows none of the default log levels.
|
|
94 |
func AllowNone() Option {
|
|
95 |
return allowed(0)
|
|
96 |
}
|
|
97 |
|
|
98 |
func allowed(allowed level) Option {
|
|
99 |
return func(l *logger) { l.allowed = allowed }
|
|
100 |
}
|
|
101 |
|
|
102 |
// 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.
|
|
105 |
func ErrNotAllowed(err error) Option {
|
|
106 |
return func(l *logger) { l.errNotAllowed = err }
|
|
107 |
}
|
|
108 |
|
|
109 |
// SquelchNoLevel instructs Log to squelch log events with no level, so that
|
|
110 |
// they don't proceed through to the wrapped logger. If SquelchNoLevel is set
|
|
111 |
// to true and a log event is squelched in this way, the error value
|
|
112 |
// configured with ErrNoLevel is returned to the caller.
|
|
113 |
func SquelchNoLevel(squelch bool) Option {
|
|
114 |
return func(l *logger) { l.squelchNoLevel = squelch }
|
|
115 |
}
|
|
116 |
|
|
117 |
// 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.
|
|
120 |
func ErrNoLevel(err error) Option {
|
|
121 |
return func(l *logger) { l.errNoLevel = err }
|
|
122 |
}
|
|
123 |
|
|
124 |
const levelKey = "level"
|
|
125 |
|
|
126 |
var (
|
|
127 |
errorLevelValue = &levelValue{level: levelError, name: "error"}
|
|
128 |
warnLevelValue = &levelValue{level: levelWarn, name: "warn"}
|
|
129 |
infoLevelValue = &levelValue{level: levelInfo, name: "info"}
|
|
130 |
debugLevelValue = &levelValue{level: levelDebug, name: "debug"}
|
|
131 |
)
|
|
132 |
|
|
133 |
type level byte
|
|
134 |
|
|
135 |
const (
|
|
136 |
levelDebug level = 1 << iota
|
|
137 |
levelInfo
|
|
138 |
levelWarn
|
|
139 |
levelError
|
|
140 |
)
|
|
141 |
|
|
142 |
type levelValue struct {
|
|
143 |
name string
|
|
144 |
level
|
|
145 |
}
|
|
146 |
|
|
147 |
func (v *levelValue) String() string {
|
|
148 |
return v.name
|
|
149 |
}
|