@@ -7,6 +7,20 @@ import (
7
7
"github.com/vimeo/alog/v3"
8
8
)
9
9
10
+ // Level represents the severity of a log message.
11
+ type Level uint8
12
+
13
+ const (
14
+ Debug Level = iota // debug
15
+ Info // info
16
+ Warning // warning
17
+ Error // error
18
+ Critical // critical
19
+ )
20
+
21
+ // LevelKey is the tag key associated with a level.
22
+ const LevelKey = "level"
23
+
10
24
// Logger is an interface that implements logging functions for different levels of severity.
11
25
type Logger interface {
12
26
// Debug logs debugging or trace information.
@@ -25,8 +39,20 @@ type Logger interface {
25
39
Critical (ctx context.Context , f string , v ... interface {})
26
40
}
27
41
42
+ // FilteredLogger amends the Logger interface with a Log method that accepts the
43
+ // level to log at.
44
+ type FilteredLogger interface {
45
+ Logger
46
+ Log (ctx context.Context , level Level , f string , v ... interface {})
47
+ SetMinLevel (level Level )
48
+ }
49
+
28
50
type defaultLogger struct {
29
51
* alog.Logger
52
+
53
+ // Indicates the minimum level to log at. If MinLevel is greater than the
54
+ // level of a given log message, the log message will be suppressed.
55
+ MinLevel Level
30
56
}
31
57
32
58
// Default returns a Logger that wraps the provided `alog.Logger`.
@@ -39,32 +65,49 @@ func Default(logger *alog.Logger) Logger {
39
65
}
40
66
}
41
67
68
+ // Filtered returns a logger that allows for setting the minimum level.
69
+ func Filtered (logger * alog.Logger ) FilteredLogger {
70
+ return & defaultLogger {
71
+ Logger : logger ,
72
+ }
73
+ }
74
+
42
75
// Debug implements Logger.Debug
43
76
func (d * defaultLogger ) Debug (ctx context.Context , f string , v ... interface {}) {
44
- ctx = alog .AddTags (ctx , "level" , "debug" )
45
- d .Logger .Output (ctx , 3 , fmt .Sprintf (f , v ... ))
77
+ d .Log (ctx , Debug , f , v ... )
46
78
}
47
79
48
80
// Info implements Logger.Info
49
81
func (d * defaultLogger ) Info (ctx context.Context , f string , v ... interface {}) {
50
- ctx = alog .AddTags (ctx , "level" , "info" )
51
- d .Logger .Output (ctx , 3 , fmt .Sprintf (f , v ... ))
82
+ d .Log (ctx , Info , f , v ... )
52
83
}
53
84
54
85
// Warning implements Logger.Warning
55
86
func (d * defaultLogger ) Warning (ctx context.Context , f string , v ... interface {}) {
56
- ctx = alog .AddTags (ctx , "level" , "warning" )
57
- d .Logger .Output (ctx , 3 , fmt .Sprintf (f , v ... ))
87
+ d .Log (ctx , Warning , f , v ... )
58
88
}
59
89
60
90
// Error implements Logger.Error
61
91
func (d * defaultLogger ) Error (ctx context.Context , f string , v ... interface {}) {
62
- ctx = alog .AddTags (ctx , "level" , "error" )
63
- d .Logger .Output (ctx , 3 , fmt .Sprintf (f , v ... ))
92
+ d .Log (ctx , Error , f , v ... )
64
93
}
65
94
66
95
// Critical implements Logger.Critical
67
96
func (d * defaultLogger ) Critical (ctx context.Context , f string , v ... interface {}) {
68
- ctx = alog .AddTags (ctx , "level" , "critical" )
69
- d .Logger .Output (ctx , 3 , fmt .Sprintf (f , v ... ))
97
+ d .Log (ctx , Critical , f , v ... )
70
98
}
99
+
100
+ // Log implements FilteredLogger.Log
101
+ func (d * defaultLogger ) Log (ctx context.Context , level Level , f string , v ... interface {}) {
102
+ if level >= d .MinLevel {
103
+ ctx = alog .AddTags (ctx , LevelKey , level .String ())
104
+ d .Logger .Output (ctx , 3 , fmt .Sprintf (f , v ... ))
105
+ }
106
+ }
107
+
108
+ // SetMinLevel sets the minimum level that will be logged and implements FilteredLogger.
109
+ func (d * defaultLogger ) SetMinLevel (level Level ) {
110
+ d .MinLevel = level
111
+ }
112
+
113
+ //go:generate go run golang.org/x/tools/cmd/stringer@latest -type Level -linecomment
0 commit comments