forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
84 lines (74 loc) · 2.03 KB
/
logger.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
package log
import (
"errors"
"fmt"
"io"
"time"
)
var (
errEmptyLoggerName = errors.New("cannot have empty logger name")
errSubLoggerAlreadyregistered = errors.New("sub logger already registered")
)
func newLogger(c *Config) Logger {
return Logger{
Timestamp: c.AdvancedSettings.TimeStampFormat,
Spacer: c.AdvancedSettings.Spacer,
ErrorHeader: c.AdvancedSettings.Headers.Error,
InfoHeader: c.AdvancedSettings.Headers.Info,
WarnHeader: c.AdvancedSettings.Headers.Warn,
DebugHeader: c.AdvancedSettings.Headers.Debug,
ShowLogSystemName: *c.AdvancedSettings.ShowLogSystemName,
}
}
func (l *Logger) newLogEvent(data, header, slName string, w io.Writer) error {
if w == nil {
return errors.New("io.Writer not set")
}
pool, ok := eventPool.Get().(*[]byte)
if !ok {
return errors.New("unable to type assert slice of bytes pointer")
}
*pool = append(*pool, header...)
if l.ShowLogSystemName {
*pool = append(*pool, l.Spacer...)
*pool = append(*pool, slName...)
}
*pool = append(*pool, l.Spacer...)
if l.Timestamp != "" {
*pool = time.Now().AppendFormat(*pool, l.Timestamp)
}
*pool = append(*pool, l.Spacer...)
*pool = append(*pool, data...)
if data == "" || data[len(data)-1] != '\n' {
*pool = append(*pool, '\n')
}
_, err := w.Write(*pool)
*pool = (*pool)[:0]
eventPool.Put(pool)
return err
}
// CloseLogger is called on shutdown of application
func CloseLogger() error {
return GlobalLogFile.Close()
}
// Level retries the current sublogger levels
func Level(name string) (Levels, error) {
RWM.RLock()
defer RWM.RUnlock()
subLogger, found := SubLoggers[name]
if !found {
return Levels{}, fmt.Errorf("logger %s not found", name)
}
return subLogger.levels, nil
}
// SetLevel sets sublogger levels
func SetLevel(s, level string) (Levels, error) {
RWM.Lock()
defer RWM.Unlock()
subLogger, found := SubLoggers[s]
if !found {
return Levels{}, fmt.Errorf("sub logger %v not found", s)
}
subLogger.SetLevels(splitLevel(level))
return subLogger.levels, nil
}