@@ -5,86 +5,88 @@ import (
5
5
)
6
6
7
7
var (
8
- // LevelKey is the key part of a level keyval.
9
- LevelKey = "level"
10
-
11
- // ErrorLevelValue is the val part of an error-level keyval.
12
- ErrorLevelValue = "error"
13
-
14
- // WarnLevelValue is the val part of a warn-level keyval.
15
- WarnLevelValue = "warn"
16
-
17
- // InfoLevelValue is the val part of an info-level keyval.
18
- InfoLevelValue = "info"
19
-
20
- // DebugLevelValue is the val part of a debug-level keyval.
21
- DebugLevelValue = "debug"
8
+ levelKey = "level"
9
+ errorLevelValue = "error"
10
+ warnLevelValue = "warn"
11
+ infoLevelValue = "info"
12
+ debugLevelValue = "debug"
22
13
)
23
14
24
- var (
25
- // AllowAll is an alias for AllowDebugAndAbove.
26
- AllowAll = AllowDebugAndAbove
15
+ // AllowAll is an alias for AllowDebugAndAbove.
16
+ func AllowAll () []string {
17
+ return AllowDebugAndAbove ()
18
+ }
27
19
28
- // AllowDebugAndAbove allows all of the four default log levels.
29
- // It may be provided as the Allowed parameter in the Config struct.
30
- AllowDebugAndAbove = []string {ErrorLevelValue , WarnLevelValue , InfoLevelValue , DebugLevelValue }
20
+ // AllowDebugAndAbove allows all of the four default log levels.
21
+ // Its return value may be provided as the Allowed parameter in the Config.
22
+ func AllowDebugAndAbove () []string {
23
+ return []string {errorLevelValue , warnLevelValue , infoLevelValue , debugLevelValue }
24
+ }
31
25
32
- // AllowInfoAndAbove allows the default info, warn, and error log levels.
33
- // It may be provided as the Allowed parameter in the Config struct.
34
- AllowInfoAndAbove = []string {ErrorLevelValue , WarnLevelValue , InfoLevelValue }
26
+ // AllowInfoAndAbove allows the default info, warn, and error log levels.
27
+ // Its return value may be provided as the Allowed parameter in the Config.
28
+ func AllowInfoAndAbove () []string {
29
+ return []string {errorLevelValue , warnLevelValue , infoLevelValue }
30
+ }
35
31
36
- // AllowWarnAndAbove allows the default warn and error log levels.
37
- // It may be provided as the Allowed parameter in the Config struct.
38
- AllowWarnAndAbove = []string {ErrorLevelValue , WarnLevelValue }
32
+ // AllowWarnAndAbove allows the default warn and error log levels.
33
+ // Its return value may be provided as the Allowed parameter in the Config.
34
+ func AllowWarnAndAbove () []string {
35
+ return []string {errorLevelValue , warnLevelValue }
36
+ }
39
37
40
- // AllowErrorOnly allows only the default error log level.
41
- // It may be provided as the Allowed parameter in the Config struct.
42
- AllowErrorOnly = []string {ErrorLevelValue }
38
+ // AllowErrorOnly allows only the default error log level.
39
+ // Its return value may be provided as the Allowed parameter in the Config.
40
+ func AllowErrorOnly () []string {
41
+ return []string {errorLevelValue }
42
+ }
43
43
44
- // AllowNone allows none of the default log levels.
45
- // It may be provided as the Allowed parameter in the Config struct.
46
- AllowNone = []string {}
47
- )
44
+ // AllowNone allows none of the default log levels.
45
+ // Its return value may be provided as the Allowed parameter in the Config.
46
+ func AllowNone () []string {
47
+ return []string {}
48
+ }
48
49
49
- // Error returns a logger with the LevelKey set to ErrorLevelValue.
50
+ // Error returns a logger with the level key set to ErrorLevelValue.
50
51
func Error (logger log.Logger ) log.Logger {
51
- return log .NewContext (logger ).With (LevelKey , ErrorLevelValue )
52
+ return log .NewContext (logger ).With (levelKey , errorLevelValue )
52
53
}
53
54
54
- // Warn returns a logger with the LevelKey set to WarnLevelValue.
55
+ // Warn returns a logger with the level key set to WarnLevelValue.
55
56
func Warn (logger log.Logger ) log.Logger {
56
- return log .NewContext (logger ).With (LevelKey , WarnLevelValue )
57
+ return log .NewContext (logger ).With (levelKey , warnLevelValue )
57
58
}
58
59
59
- // Info returns a logger with the LevelKey set to InfoLevelValue.
60
+ // Info returns a logger with the level key set to InfoLevelValue.
60
61
func Info (logger log.Logger ) log.Logger {
61
- return log .NewContext (logger ).With (LevelKey , InfoLevelValue )
62
+ return log .NewContext (logger ).With (levelKey , infoLevelValue )
62
63
}
63
64
64
- // Debug returns a logger with the LevelKey set to DebugLevelValue.
65
+ // Debug returns a logger with the level key set to DebugLevelValue.
65
66
func Debug (logger log.Logger ) log.Logger {
66
- return log .NewContext (logger ).With (LevelKey , DebugLevelValue )
67
+ return log .NewContext (logger ).With (levelKey , debugLevelValue )
67
68
}
68
69
69
70
// Config parameterizes the leveled logger.
70
71
type Config struct {
71
72
// Allowed enumerates the accepted log levels. If a log event is encountered
72
- // with a LevelKey set to a value that isn't explicitly allowed, the event
73
- // will be squelched, and ErrSquelched returned.
73
+ // with a level key set to a value that isn't explicitly allowed, the event
74
+ // will be squelched, and ErrNotAllowed returned.
74
75
Allowed []string
75
76
76
- // ErrSquelched is returned to the caller when Log is invoked with a
77
- // LevelKey that hasn't been explicitly allowed. By default, ErrSquelched is
77
+ // ErrNotAllowed is returned to the caller when Log is invoked with a level
78
+ // key that hasn't been explicitly allowed. By default, ErrNotAllowed is
78
79
// nil; in this case, the log event is squelched with no error.
79
- ErrSquelched error
80
+ ErrNotAllowed error
80
81
81
- // AllowNoLevel will allow log events with no LevelKey to proceed through to
82
- // the wrapped logger without error. By default, log events with no LevelKey
83
- // will be squelched, and ErrNoLevel returned.
84
- AllowNoLevel bool
82
+ // SquelchNoLevel will squelch log events with no level key, so that they
83
+ // don't proceed through to the wrapped logger. If SquelchNoLevel is set to
84
+ // true and a log event is squelched in this way, ErrNoLevel is returned to
85
+ // the caller.
86
+ SquelchNoLevel bool
85
87
86
- // ErrNoLevel is returned to the caller when AllowNoLevel is false , and Log
87
- // is invoked without a LevelKey . By default, ErrNoLevel is nil; in this
88
+ // ErrNoLevel is returned to the caller when SquelchNoLevel is true , and Log
89
+ // is invoked without a level key . By default, ErrNoLevel is nil; in this
88
90
// case, the log event is squelched with no error.
89
91
ErrNoLevel error
90
92
}
@@ -93,26 +95,26 @@ type Config struct {
93
95
// Config object for a detailed description of how to configure levels.
94
96
func New (next log.Logger , config Config ) log.Logger {
95
97
return & logger {
96
- next : next ,
97
- allowed : makeSet (config .Allowed ),
98
- errSquelched : config .ErrSquelched ,
99
- allowNoLevel : config .AllowNoLevel ,
100
- errNoLevel : config .ErrNoLevel ,
98
+ next : next ,
99
+ allowed : makeSet (config .Allowed ),
100
+ errNotAllowed : config .ErrNotAllowed ,
101
+ squelchNoLevel : config .SquelchNoLevel ,
102
+ errNoLevel : config .ErrNoLevel ,
101
103
}
102
104
}
103
105
104
106
type logger struct {
105
- next log.Logger
106
- allowed map [string ]struct {}
107
- errSquelched error
108
- allowNoLevel bool
109
- errNoLevel error
107
+ next log.Logger
108
+ allowed map [string ]struct {}
109
+ errNotAllowed error
110
+ squelchNoLevel bool
111
+ errNoLevel error
110
112
}
111
113
112
114
func (l * logger ) Log (keyvals ... interface {}) error {
113
115
var hasLevel , levelAllowed bool
114
116
for i := 0 ; i < len (keyvals ); i += 2 {
115
- if k , ok := keyvals [i ].(string ); ! ok || k != LevelKey {
117
+ if k , ok := keyvals [i ].(string ); ! ok || k != levelKey {
116
118
continue
117
119
}
118
120
hasLevel = true
@@ -126,11 +128,11 @@ func (l *logger) Log(keyvals ...interface{}) error {
126
128
_ , levelAllowed = l .allowed [v ]
127
129
break
128
130
}
129
- if ! hasLevel && ! l . allowNoLevel {
131
+ if ! hasLevel && l . squelchNoLevel {
130
132
return l .errNoLevel
131
133
}
132
134
if hasLevel && ! levelAllowed {
133
- return l .errSquelched
135
+ return l .errNotAllowed
134
136
}
135
137
return l .next .Log (keyvals ... )
136
138
}
0 commit comments