forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_setup.go
163 lines (142 loc) · 3.84 KB
/
logger_setup.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
package log
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/thrasher-corp/gocryptotrader/common/convert"
)
func getWriters(s *SubLoggerConfig) io.Writer {
mw := MultiWriter()
m := mw.(*multiWriter)
outputWriters := strings.Split(s.Output, "|")
for x := range outputWriters {
switch outputWriters[x] {
case "stdout", "console":
m.Add(os.Stdout)
case "stderr":
m.Add(os.Stderr)
case "file":
if FileLoggingConfiguredCorrectly {
m.Add(GlobalLogFile)
}
default:
m.Add(ioutil.Discard)
}
}
return m
}
// GenDefaultSettings return struct with known sane/working logger settings
func GenDefaultSettings() (log Config) {
log = Config{
Enabled: convert.BoolPtr(true),
SubLoggerConfig: SubLoggerConfig{
Level: "INFO|DEBUG|WARN|ERROR",
Output: "console",
},
LoggerFileConfig: &loggerFileConfig{
FileName: "log.txt",
Rotate: convert.BoolPtr(false),
MaxSize: 0,
},
AdvancedSettings: advancedSettings{
ShowLogSystemName: convert.BoolPtr(false),
Spacer: spacer,
TimeStampFormat: timestampFormat,
Headers: headers{
Info: "[INFO]",
Warn: "[WARN]",
Debug: "[DEBUG]",
Error: "[ERROR]",
},
},
}
return
}
func configureSubLogger(logger, levels string, output io.Writer) error {
found, logPtr := validSubLogger(logger)
if !found {
return fmt.Errorf("logger %v not found", logger)
}
logPtr.output = output
logPtr.Levels = splitLevel(levels)
subLoggers[logger] = logPtr
return nil
}
// SetupSubLoggers configure all sub loggers with provided configuration values
func SetupSubLoggers(s []SubLoggerConfig) {
for x := range s {
output := getWriters(&s[x])
err := configureSubLogger(strings.ToUpper(s[x].Name), s[x].Level, output)
if err != nil {
continue
}
}
}
// SetupGlobalLogger setup the global loggers with the default global config values
func SetupGlobalLogger() {
RWM.Lock()
if FileLoggingConfiguredCorrectly {
GlobalLogFile = &Rotate{
FileName: GlobalLogConfig.LoggerFileConfig.FileName,
MaxSize: GlobalLogConfig.LoggerFileConfig.MaxSize,
Rotate: GlobalLogConfig.LoggerFileConfig.Rotate,
}
}
for x := range subLoggers {
subLoggers[x].Levels = splitLevel(GlobalLogConfig.Level)
subLoggers[x].output = getWriters(&GlobalLogConfig.SubLoggerConfig)
}
logger = newLogger(GlobalLogConfig)
RWM.Unlock()
}
func splitLevel(level string) (l Levels) {
enabledLevels := strings.Split(level, "|")
for x := range enabledLevels {
switch level := enabledLevels[x]; level {
case "DEBUG":
l.Debug = true
case "INFO":
l.Info = true
case "WARN":
l.Warn = true
case "ERROR":
l.Error = true
}
}
return
}
func registerNewSubLogger(logger string) *subLogger {
temp := subLogger{
name: strings.ToUpper(logger),
output: os.Stdout,
}
temp.Levels = splitLevel("INFO|WARN|DEBUG|ERROR")
subLoggers[logger] = &temp
return &temp
}
// register all loggers at package init()
func init() {
Global = registerNewSubLogger("LOG")
ConnectionMgr = registerNewSubLogger("CONNECTION")
BackTester = registerNewSubLogger("BACKTESTER")
CommunicationMgr = registerNewSubLogger("COMMS")
ConfigMgr = registerNewSubLogger("CONFIG")
DatabaseMgr = registerNewSubLogger("DATABASE")
OrderMgr = registerNewSubLogger("ORDER")
PortfolioMgr = registerNewSubLogger("PORTFOLIO")
SyncMgr = registerNewSubLogger("SYNC")
TimeMgr = registerNewSubLogger("TIMEKEEPER")
GCTScriptMgr = registerNewSubLogger("GCTSCRIPT")
WebsocketMgr = registerNewSubLogger("WEBSOCKET")
EventMgr = registerNewSubLogger("EVENT")
DispatchMgr = registerNewSubLogger("DISPATCH")
RequestSys = registerNewSubLogger("REQUESTER")
ExchangeSys = registerNewSubLogger("EXCHANGE")
GRPCSys = registerNewSubLogger("GRPC")
RESTSys = registerNewSubLogger("REST")
Ticker = registerNewSubLogger("TICKER")
OrderBook = registerNewSubLogger("ORDERBOOK")
Trade = registerNewSubLogger("TRADE")
}