-
Notifications
You must be signed in to change notification settings - Fork 662
/
factory.go
185 lines (148 loc) · 4.58 KB
/
factory.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package logging
import (
"fmt"
"os"
"path"
"sync"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
var _ Factory = &factory{}
// Factory creates new instances of different types of Logger
type Factory interface {
// Make creates a new logger with name [name]
Make(name string) (Logger, error)
// MakeChain creates a new logger to log the events of chain [chainID]
MakeChain(chainID string) (Logger, error)
// SetLogLevels sets log levels for all loggers in factory with given logger name, level pairs.
SetLogLevel(name string, level Level) error
// SetDisplayLevels sets log display levels for all loggers in factory with given logger name, level pairs.
SetDisplayLevel(name string, level Level) error
// GetLogLevels returns all log levels in factory as name, level pairs
GetLogLevel(name string) (Level, error)
// GetDisplayLevels returns all log display levels in factory as name, level pairs
GetDisplayLevel(name string) (Level, error)
// GetLoggerNames returns the names of all logs created by this factory
GetLoggerNames() []string
// Close stops and clears all of a Factory's instantiated loggers
Close()
}
type logWrapper struct {
logger Logger
displayLevel zap.AtomicLevel
logLevel zap.AtomicLevel
}
type factory struct {
config Config
lock sync.RWMutex
// For each logger created by this factory:
// Logger name --> the logger.
loggers map[string]logWrapper
}
// NewFactory returns a new instance of a Factory producing loggers configured with
// the values set in the [config] parameter
func NewFactory(config Config) Factory {
return &factory{
config: config,
loggers: make(map[string]logWrapper),
}
}
// Assumes [f.lock] is held
func (f *factory) makeLogger(config Config) (Logger, error) {
if _, ok := f.loggers[config.LoggerName]; ok {
return nil, fmt.Errorf("logger with name %q already exists", config.LoggerName)
}
consoleEnc := config.LogFormat.ConsoleEncoder()
fileEnc := config.LogFormat.FileEncoder()
consoleCore := NewWrappedCore(config.DisplayLevel, os.Stdout, consoleEnc)
consoleCore.WriterDisabled = config.DisableWriterDisplaying
rw := &lumberjack.Logger{
Filename: path.Join(config.Directory, config.LoggerName+".log"),
MaxSize: config.MaxSize, // megabytes
MaxAge: config.MaxAge, // days
MaxBackups: config.MaxFiles, // files
Compress: config.Compress,
}
fileCore := NewWrappedCore(config.LogLevel, rw, fileEnc)
prefix := config.LogFormat.WrapPrefix(config.MsgPrefix)
l := NewLogger(config.Assertions, prefix, consoleCore, fileCore)
f.loggers[config.LoggerName] = logWrapper{
logger: l,
displayLevel: consoleCore.AtomicLevel,
logLevel: fileCore.AtomicLevel,
}
return l, nil
}
func (f *factory) Make(name string) (Logger, error) {
f.lock.Lock()
defer f.lock.Unlock()
config := f.config
config.LoggerName = name
return f.makeLogger(config)
}
func (f *factory) MakeChain(chainID string) (Logger, error) {
f.lock.Lock()
defer f.lock.Unlock()
config := f.config
config.MsgPrefix = chainID + " Chain"
config.LoggerName = chainID
return f.makeLogger(config)
}
func (f *factory) SetLogLevel(name string, level Level) error {
f.lock.RLock()
defer f.lock.RUnlock()
logger, ok := f.loggers[name]
if !ok {
return fmt.Errorf("logger with name %q not found", name)
}
logger.logLevel.SetLevel(zapcore.Level(level))
return nil
}
func (f *factory) SetDisplayLevel(name string, level Level) error {
f.lock.RLock()
defer f.lock.RUnlock()
logger, ok := f.loggers[name]
if !ok {
return fmt.Errorf("logger with name %q not found", name)
}
logger.displayLevel.SetLevel(zapcore.Level(level))
return nil
}
func (f *factory) GetLogLevel(name string) (Level, error) {
f.lock.RLock()
defer f.lock.RUnlock()
logger, ok := f.loggers[name]
if !ok {
return -1, fmt.Errorf("logger with name %q not found", name)
}
return Level(logger.logLevel.Level()), nil
}
func (f *factory) GetDisplayLevel(name string) (Level, error) {
f.lock.RLock()
defer f.lock.RUnlock()
logger, ok := f.loggers[name]
if !ok {
return -1, fmt.Errorf("logger with name %q not found", name)
}
return Level(logger.displayLevel.Level()), nil
}
func (f *factory) GetLoggerNames() []string {
f.lock.RLock()
defer f.lock.RUnlock()
names := make([]string, 0, len(f.loggers))
for name := range f.loggers {
names = append(names, name)
}
return names
}
func (f *factory) Close() {
f.lock.Lock()
defer f.lock.Unlock()
for _, lw := range f.loggers {
lw.logger.Stop()
}
f.loggers = nil
}