-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlevelswitch.go
More file actions
75 lines (62 loc) · 2.14 KB
/
levelswitch.go
File metadata and controls
75 lines (62 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package mtlog
import (
"sync/atomic"
"github.com/willibrandon/mtlog/core"
)
// LoggingLevelSwitch provides thread-safe, runtime control of the minimum log level.
// It enables dynamic adjustment of logging levels without restarting the application.
type LoggingLevelSwitch struct {
// level is stored as int32 to enable atomic operations
// We use int32 because core.LogEventLevel is an int32 underneath
level int32
}
// NewLoggingLevelSwitch creates a new logging level switch with the specified initial level.
func NewLoggingLevelSwitch(initialLevel core.LogEventLevel) *LoggingLevelSwitch {
ls := &LoggingLevelSwitch{}
ls.SetLevel(initialLevel)
return ls
}
// Level returns the current minimum log level.
func (ls *LoggingLevelSwitch) Level() core.LogEventLevel {
return core.LogEventLevel(atomic.LoadInt32(&ls.level))
}
// SetLevel updates the minimum log level.
// This operation is thread-safe and takes effect immediately.
func (ls *LoggingLevelSwitch) SetLevel(level core.LogEventLevel) {
atomic.StoreInt32(&ls.level, int32(level))
}
// IsEnabled returns true if the specified level would be processed
// with the current minimum level setting.
func (ls *LoggingLevelSwitch) IsEnabled(level core.LogEventLevel) bool {
return level >= ls.Level()
}
// Verbose sets the minimum level to Verbose.
func (ls *LoggingLevelSwitch) Verbose() *LoggingLevelSwitch {
ls.SetLevel(core.VerboseLevel)
return ls
}
// Debug sets the minimum level to Debug.
func (ls *LoggingLevelSwitch) Debug() *LoggingLevelSwitch {
ls.SetLevel(core.DebugLevel)
return ls
}
// Information sets the minimum level to Information.
func (ls *LoggingLevelSwitch) Information() *LoggingLevelSwitch {
ls.SetLevel(core.InformationLevel)
return ls
}
// Warning sets the minimum level to Warning.
func (ls *LoggingLevelSwitch) Warning() *LoggingLevelSwitch {
ls.SetLevel(core.WarningLevel)
return ls
}
// Error sets the minimum level to Error.
func (ls *LoggingLevelSwitch) Error() *LoggingLevelSwitch {
ls.SetLevel(core.ErrorLevel)
return ls
}
// Fatal sets the minimum level to Fatal.
func (ls *LoggingLevelSwitch) Fatal() *LoggingLevelSwitch {
ls.SetLevel(core.FatalLevel)
return ls
}