-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
79 lines (67 loc) · 1.99 KB
/
config.go
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
76
77
78
79
package logger
import (
"errors"
"sync"
"go.uber.org/zap/zapcore"
)
// factoryConfig is the configuration for the logger
type factoryConfig struct {
rootLevel int // the level for the root logger
enableNameInLog bool // whether to include the logger name in the log message
enableStackTrace bool // for fatal logs
levelConfig *syncMap[string, int] // preconfigured log levels for loggers
levelConfigCache *syncMap[string, int] // cache of all calculated log levels for loggers
// zap specific config
clock zapcore.Clock
}
// SetLogLevel sets the log level for the given logger name
func (fc *factoryConfig) SetLogLevel(name, levelStr string) error {
level, ok := levelMap[levelStr]
if !ok {
return errors.New("invalid level value : " + levelStr)
}
if name == "" {
fc.rootLevel = level
} else {
fc.levelConfig.set(name, level)
}
fc.levelConfigCache = newSyncMap[string, int]()
return nil
}
// getOrSetLogLevel returns the log level for the given logger name or sets it using the provided function if no level is set
func (fc *factoryConfig) getOrSetLogLevel(name string, parentLevelFunc func() int) int {
if name == "" {
return fc.rootLevel
}
if level, found := fc.levelConfigCache.get(name); found {
return level
}
level := func() int { // either get the level from the config or use the parent's level
if level, ok := fc.levelConfig.get(name); ok {
return level
}
return parentLevelFunc()
}()
fc.levelConfigCache.set(name, level) // cache the level
return level
}
// newSyncMap creates a new syncMap
func newSyncMap[K comparable, V any]() *syncMap[K, V] {
return &syncMap[K, V]{m: map[K]V{}}
}
// syncMap is a thread safe map for getting and setting keys concurrently
type syncMap[K comparable, V any] struct {
mu sync.RWMutex
m map[K]V
}
func (sm *syncMap[K, V]) get(key K) (V, bool) {
sm.mu.RLock()
defer sm.mu.RUnlock()
v, ok := sm.m[key]
return v, ok
}
func (sm *syncMap[K, V]) set(key K, value V) {
sm.mu.Lock()
defer sm.mu.Unlock()
sm.m[key] = value
}