-
Notifications
You must be signed in to change notification settings - Fork 8
/
logs.go
224 lines (196 loc) · 3.96 KB
/
logs.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package logs
import (
"context"
"io"
"sync"
"github.com/zxysilent/logs/internal/buffer"
"github.com/zxysilent/logs/internal/encoder"
"github.com/zxysilent/logs/internal/encoder/json"
"github.com/zxysilent/logs/internal/encoder/text"
"github.com/zxysilent/logs/internal/file"
)
// 日志等级
type logLevel int
const (
LDEBUG logLevel = iota
LINFO
LWARN
LERROR
logShort = "DBGINFWRNERR" //DBG INF WRN ERR
)
const (
timeFieldName = "time"
traceFieldName = "trace"
levelFieldName = "level"
msgFieldName = "msg"
errorFieldName = "error"
callerFieldName = "caller"
timeFieldFormat = "2006/01/02 15:04:05.000"
)
// 字符串等级
func (lv logLevel) String() string {
return logShort[lv*3 : lv*3+3]
}
type Logger struct {
out io.Writer // 输出
sep string // 路径分隔
caller bool // 调用信息
level logLevel // 日志等级
skip int //
enc encoder.Encoder //
mu sync.Mutex // logger🔒
fw *file.Writer
}
func New(out io.Writer) *Logger {
if out == nil {
out = io.Discard
}
n := &Logger{
out: out,
caller: false,
level: LINFO,
skip: 0,
sep: "/",
}
n.enc = &text.Encoder{}
return n
}
func (l *Logger) SetFile(path string) {
l.fw = file.New(path)
l.SetOutput(l.fw)
}
// SetMaxAge 最大保留天数
func (l *Logger) SetMaxAge(ma int) {
if l.fw == nil {
return
}
l.mu.Lock()
defer l.mu.Unlock()
l.fw.SetMaxAge(ma)
}
// SetMaxSize 单个日志最大容量
func (l *Logger) SetMaxSize(ms int64) {
if l.fw == nil {
return
}
l.mu.Lock()
defer l.mu.Unlock()
l.fw.SetMaxSize(ms)
}
// SetCons 同时输出控制台
func (l *Logger) SetCons(b bool) {
if l.fw == nil {
return
}
l.mu.Lock()
defer l.mu.Unlock()
l.fw.SetCons(b)
}
func (l *Logger) Close() error {
l.mu.Lock()
defer l.mu.Unlock()
if l.fw != nil {
return l.fw.Close()
}
return nil
}
// 设置输出等级
func (l *Logger) SetLevel(lv logLevel) {
if lv < LDEBUG || lv > LERROR {
panic("logs: illegal log level")
}
l.mu.Lock()
l.level = lv
l.mu.Unlock()
}
// 设置调用信息
func (l *Logger) SetCaller(b bool) {
l.mu.Lock()
l.caller = b
l.mu.Unlock()
}
func (l *Logger) SetJSON() {
l.enc = &json.Encoder{}
}
func (l *Logger) SetText() {
l.enc = &text.Encoder{}
}
func (l *Logger) SetSep(sep string) {
l.mu.Lock()
l.sep = sep
l.mu.Unlock()
}
func (l *Logger) SetSkip(skip int) {
l.mu.Lock()
l.skip = skip
l.mu.Unlock()
}
func (l *Logger) SetOutput(out io.Writer) {
l.mu.Lock()
l.out = out
l.mu.Unlock()
}
func (l *Logger) Write(p []byte) (int, error) {
return l.out.Write(p)
}
func (l *Logger) With() *FieldLogger {
f := &FieldLogger{}
f.enc = l.enc
f.logger = l
f.caller = l.caller
f.attr = buffer.Get()
return f
}
// tracing
func (l *Logger) Ctx(ctx context.Context) *FieldLogger {
f := &FieldLogger{}
f.enc = l.enc
f.trace, _ = ctx.Value(traceKey).(string)
f.logger = l
f.caller = l.caller
f.attr = buffer.Get()
return f
}
func (l *Logger) Debug(args ...any) {
if LDEBUG >= l.level {
print("", LDEBUG, l.caller, l, nil, args...)
}
}
func (l *Logger) Debugf(foramt string, args ...any) {
if LDEBUG >= l.level {
printf("", LDEBUG, l.caller, l, nil, foramt, args...)
}
}
func (l *Logger) Info(args ...any) {
if LINFO >= l.level {
print("", LINFO, l.caller, l, nil, args...)
}
}
func (l *Logger) Infof(foramt string, args ...any) {
if LINFO >= l.level {
printf("", LINFO, l.caller, l, nil, foramt, args...)
}
}
func (l *Logger) Warn(args ...any) {
if LWARN >= l.level {
print("", LWARN, l.caller, l, nil, args...)
}
}
func (l *Logger) Warnf(foramt string, args ...any) {
if LWARN >= l.level {
printf("", LWARN, l.caller, l, nil, foramt, args...)
}
}
func (l *Logger) Error(args ...any) {
if LERROR >= l.level {
print("", LERROR, l.caller, l, nil, args...)
}
}
func (l *Logger) Errorf(foramt string, args ...any) {
if LERROR >= l.level {
printf("", LERROR, l.caller, l, nil, foramt, args...)
}
}
func (l *Logger) Writer() io.Writer {
return l
}