-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathderivedconfig.go
136 lines (114 loc) · 3.36 KB
/
derivedconfig.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package golog
import (
"context"
"sync"
)
// Ensure that DerivedConfig implements Config
var _ Config = new(DerivedConfig)
// DerivedConfig wraps another changable Config by saving a pointer
// to a variable of type Config. That variable can be changed at runtime
// so it doesn't have to be initialized at the momenht of the DerivedConfig creation.
//
// A DerivedConfig can have its own LevelFilter, which will be used to decide
// if a log message should be written or not. If the DerivedConfig has no filter,
// the filter of the parent Config will be used.
type DerivedConfig struct {
parent *Config
filter *LevelFilter
addWriterConfigs []WriterConfig
mutex sync.RWMutex
}
func NewDerivedConfig(parent *Config) *DerivedConfig {
if parent == nil || *parent == nil {
panic("golog.DerivedConfig parent must not be nil")
}
return &DerivedConfig{
parent: parent,
}
}
func NewDerivedConfigWithFilter(parent *Config, filters ...LevelFilter) *DerivedConfig {
if parent == nil || *parent == nil {
panic("golog.DerivedConfig parent must not be nil")
}
return &DerivedConfig{
parent: parent,
filter: newLevelFilterOrNil(filters),
}
}
func NewDerivedConfigWithAdditionalWriterConfigs(parent *Config, configs ...WriterConfig) *DerivedConfig {
if parent == nil || *parent == nil {
panic("golog.DerivedConfig parent must not be nil")
}
return &DerivedConfig{
parent: parent,
addWriterConfigs: uniqueWriterConfigs(append((*parent).WriterConfigs(), configs...)),
}
}
func (c *DerivedConfig) Parent() Config {
c.mutex.RLock()
defer c.mutex.RUnlock()
return *c.parent
}
func (c *DerivedConfig) SetParent(parent *Config) {
if parent == nil || *parent == nil {
panic("golog.DerivedConfig parent must not be nil")
}
c.mutex.Lock()
c.parent = parent
c.mutex.Unlock()
}
func (c *DerivedConfig) SetFilter(filters ...LevelFilter) {
c.mutex.Lock()
c.filter = newLevelFilterOrNil(filters)
c.mutex.Unlock()
}
func (c *DerivedConfig) SetAdditionalWriterConfigs(configs ...WriterConfig) {
c.mutex.Lock()
if len(configs) == 0 {
c.addWriterConfigs = nil
} else {
c.addWriterConfigs = uniqueWriterConfigs(append((*c.parent).WriterConfigs(), configs...))
}
c.mutex.Unlock()
}
func (c *DerivedConfig) WriterConfigs() []WriterConfig {
c.mutex.RLock()
defer c.mutex.RUnlock()
if c.addWriterConfigs != nil {
// If DerivedConfig has its own writer configs, use them
return c.addWriterConfigs
}
// Else use the writer configs of the parent Config
return (*c.parent).WriterConfigs()
}
func (c *DerivedConfig) Levels() *Levels {
return (*c.parent).Levels()
}
func (c *DerivedConfig) IsActive(ctx context.Context, level Level) bool {
c.mutex.RLock()
defer c.mutex.RUnlock()
if c.filter != nil {
// If DerivedConfig has its own filter, use it
return c.filter.IsActive(ctx, level)
}
// Else use the filter of the parent Config
return (*c.parent).IsActive(ctx, level)
}
func (c *DerivedConfig) FatalLevel() Level {
return (*c.parent).FatalLevel()
}
func (c *DerivedConfig) ErrorLevel() Level {
return (*c.parent).ErrorLevel()
}
func (c *DerivedConfig) WarnLevel() Level {
return (*c.parent).WarnLevel()
}
func (c *DerivedConfig) InfoLevel() Level {
return (*c.parent).InfoLevel()
}
func (c *DerivedConfig) DebugLevel() Level {
return (*c.parent).DebugLevel()
}
func (c *DerivedConfig) TraceLevel() Level {
return (*c.parent).TraceLevel()
}