-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslog.go
151 lines (110 loc) · 2.72 KB
/
slog.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
package log
import (
"github.com/coinrust/slog"
)
type SLogger struct {
inner *slog.Logger
}
func (l *SLogger) Debug(args ...interface{}) {
slog.Debug(args...)
}
func (l *SLogger) Info(args ...interface{}) {
slog.Info(args...)
}
func (l *SLogger) Warn(args ...interface{}) {
slog.Warn(args...)
}
func (l *SLogger) Error(args ...interface{}) {
slog.Error(args...)
}
func (l *SLogger) DPanic(args ...interface{}) {
}
func (l *SLogger) Panic(args ...interface{}) {
slog.Crit(args...)
}
func (l *SLogger) Fatal(args ...interface{}) {
slog.Crit(args...)
}
func (l *SLogger) Debugf(template string, args ...interface{}) {
slog.Debugf(template, args...)
}
func (l *SLogger) Infof(template string, args ...interface{}) {
slog.Infof(template, args...)
}
func (l *SLogger) Warnf(template string, args ...interface{}) {
slog.Warnf(template, args...)
}
func (l *SLogger) Errorf(template string, args ...interface{}) {
slog.Errorf(template, args...)
}
func (l *SLogger) DPanicf(template string, args ...interface{}) {
slog.Critf(template, args...)
}
func (l *SLogger) Panicf(template string, args ...interface{}) {
slog.Critf(template, args...)
}
func (l *SLogger) Fatalf(template string, args ...interface{}) {
slog.Critf(template, args...)
}
func (l *SLogger) Debugw(msg string, keysAndValues ...interface{}) {
}
func (l *SLogger) Infow(msg string, keysAndValues ...interface{}) {
}
func (l *SLogger) Warnw(msg string, keysAndValues ...interface{}) {
}
func (l *SLogger) Errorw(msg string, keysAndValues ...interface{}) {
}
func (l *SLogger) DPanicw(msg string, keysAndValues ...interface{}) {
}
func (l *SLogger) Panicw(msg string, keysAndValues ...interface{}) {
}
func (l *SLogger) Fatalw(msg string, keysAndValues ...interface{}) {
}
func (l *SLogger) Sync() error {
if l.inner == nil {
return nil
}
l.inner.Close()
return nil
}
func NewSLogger(c *Configuration) *SLogger {
level := parseSLogLevel(c.Level)
h := slog.NewHandler(level, slog.DefaultFormatter)
maxBackup := c.MaxBackups
if maxBackup == 0 {
maxBackup = 16
}
tw, err := slog.NewTimedRotatingFileWriter(c.Path,
slog.RotateByDate, uint8(maxBackup))
if err != nil {
return nil
}
h.AddWriter(tw)
//fw, err := slog.NewRotatingFileWriter(c.Path,
// uint64(c.MaxFileSize*1024*1024), uint8(c.MaxBackups))
//if err != nil {
// return nil
//}
//
//h.AddWriter(fw)
l := slog.NewLogger(level)
l.AddHandler(h)
slog.SetDefaultLogger(l)
return &SLogger{inner: l}
}
func parseSLogLevel(level string) slog.Level {
var l slog.Level
switch level {
case DebugLevel:
l = slog.DebugLevel
case InfoLevel:
l = slog.InfoLevel
case WarnLevel:
l = slog.WarnLevel
case ErrorLevel:
l = slog.ErrorLevel
case PanicLevel:
l = slog.CritLevel
}
return l
}