forked from openware/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
82 lines (70 loc) · 1.88 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
package log
import (
"errors"
"fmt"
"io"
"time"
)
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")
}
e := eventPool.Get().(*Event)
e.output = w
e.data = append(e.data, []byte(header)...)
if l.ShowLogSystemName {
e.data = append(e.data, l.Spacer...)
e.data = append(e.data, slName...)
}
e.data = append(e.data, l.Spacer...)
if l.Timestamp != "" {
e.data = time.Now().AppendFormat(e.data, l.Timestamp)
}
e.data = append(e.data, l.Spacer...)
e.data = append(e.data, []byte(data)...)
if data == "" || data[len(data)-1] != '\n' {
e.data = append(e.data, '\n')
}
_, err := e.output.Write(e.data)
e.data = e.data[:0]
eventPool.Put(e)
return err
}
// CloseLogger is called on shutdown of application
func CloseLogger() error {
return GlobalLogFile.Close()
}
func validSubLogger(s string) (bool, *subLogger) {
if v, found := subLoggers[s]; found {
return true, v
}
return false, nil
}
// Level retries the current sublogger levels
func Level(s string) (*Levels, error) {
found, logger := validSubLogger(s)
if !found {
return nil, fmt.Errorf("logger %v not found", s)
}
return &logger.Levels, nil
}
// SetLevel sets sublogger levels
func SetLevel(s, level string) (*Levels, error) {
found, logger := validSubLogger(s)
if !found {
return nil, fmt.Errorf("logger %v not found", s)
}
logger.Levels = splitLevel(level)
return &logger.Levels, nil
}