/
derivedconfig.go
91 lines (75 loc) · 1.74 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
package golog
import (
"context"
"sync"
)
// DerivedConfig
type DerivedConfig struct {
parent *Config
filter *LevelFilter
mutex sync.Mutex
}
func NewDerivedConfig(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 (c *DerivedConfig) Parent() Config {
c.mutex.Lock()
defer c.mutex.Unlock()
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) Writer() Writer {
return (*c.parent).Writer()
}
func (c *DerivedConfig) Levels() *Levels {
return (*c.parent).Levels()
}
func (c *DerivedConfig) IsActive(ctx context.Context, level Level) bool {
var active bool
c.mutex.Lock()
if c.filter != nil {
// If DerivedConfig has its own filter, use it
active = c.filter.IsActive(ctx, level)
} else {
// else use the filter of the parent Config
active = (*c.parent).IsActive(ctx, level)
}
c.mutex.Unlock()
return active
}
func (c *DerivedConfig) Fatal() Level {
return (*c.parent).Fatal()
}
func (c *DerivedConfig) Error() Level {
return (*c.parent).Error()
}
func (c *DerivedConfig) Warn() Level {
return (*c.parent).Warn()
}
func (c *DerivedConfig) Info() Level {
return (*c.parent).Info()
}
func (c *DerivedConfig) Debug() Level {
return (*c.parent).Debug()
}
func (c *DerivedConfig) Trace() Level {
return (*c.parent).Trace()
}