-
Notifications
You must be signed in to change notification settings - Fork 4
/
alog.go
305 lines (248 loc) · 6.72 KB
/
alog.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package alog
import (
"fmt"
"os"
"github.com/antlinker/alog/utils"
"github.com/antlinker/alog/log"
"github.com/antlinker/alog/manage"
)
// ALog 提供ALog日志模块的输出管理
type ALog struct {
tag log.LogTag
config *log.LogConfig
manage log.LogManage
}
// NewALog 获取ALog实例
// configs 配置文件路径
func NewALog(configs ...string) *ALog {
defer func() {
if err := recover(); err != nil {
fmt.Println("===> [ALog]Initialization error:", err)
os.Exit(-1)
}
}()
config := loadDefaultConfig()
if len(configs) > 0 {
err := utils.NewConfig(configs[0]).Read(config)
if err != nil {
panic(err)
}
} else if gLg := GALog; gLg != nil {
config = &(*(gLg.GetConfig()))
}
return &ALog{
config: config,
manage: manage.NewLogManage(config),
tag: log.DefaultTag,
}
}
// SetLogTag 设置LogTag
func (a *ALog) SetLogTag(tag string) {
a.tag = log.LogTag(tag)
}
// SetLogLevel 设置日志输出级别
func (a *ALog) SetLogLevel(level log.LogLevel) {
a.config.Console.Level = level
a.config.Global.Level = level
}
// SetShowFile 设置输出文件信息
func (a *ALog) SetShowFile(v bool) {
show := 2
if v {
show = 1
}
a.config.Global.ShowFile = show
}
// SetFileCaller 设置文件调用层次
func (a *ALog) SetFileCaller(caller int) {
a.config.Global.FileCaller = caller
}
// SetRule 设置输出规则
func (a *ALog) SetRule(rule log.LogRule) {
a.config.Global.Rule = rule
}
// SetEnabled 设置是否启用日志
func (a *ALog) SetEnabled(enabled bool) {
v := 2
if enabled {
v = 1
}
a.config.Global.IsEnabled = v
}
// SetPrint 设置控制台输出日志
func (a *ALog) SetPrint(v bool) {
vv := 2
if v {
vv = 1
}
a.config.Global.IsPrint = vv
}
// ReloadConfig 重置加载配置文件
func (a *ALog) ReloadConfig(cfg string) error {
config := loadDefaultConfig()
err := utils.NewConfig(cfg).Read(config)
if err != nil {
return err
}
a.config = config
a.manage = manage.NewLogManage(config)
return nil
}
// GetConfig 获取配置文件信息
func (a *ALog) GetConfig() *log.LogConfig {
return a.config
}
// GetWriteNum 获取写入持久化日志条数
func (a *ALog) GetWriteNum() int64 {
return a.manage.TotalNum()
}
// Write 输出消息
func (a *ALog) Write(onlyConsole bool, level log.LogLevel, tag string, v ...interface{}) {
if a.config.Global.IsEnabled != 1 {
return
}
t := log.LogTag(tag)
if t == "" {
t = a.tag
}
if onlyConsole {
a.manage.Console(level, t, v...)
return
}
a.manage.Write(level, t, v...)
}
// Writef 输出格式化消息
func (a *ALog) Writef(onlyConsole bool, level log.LogLevel, tag string, format string, v ...interface{}) {
if a.config.Global.IsEnabled != 1 {
return
}
t := log.LogTag(tag)
if t == "" {
t = a.tag
}
if onlyConsole {
a.manage.Consolef(level, t, format, v...)
return
}
a.manage.Writef(level, t, format, v...)
}
func (a *ALog) Debug(v ...interface{}) {
a.Write(false, log.DEBUG, "", v...)
}
func (a *ALog) Debugf(format string, v ...interface{}) {
a.Writef(false, log.DEBUG, "", format, v...)
}
func (a *ALog) DebugT(tag string, v ...interface{}) {
a.Write(false, log.DEBUG, tag, v...)
}
func (a *ALog) DebugTf(tag string, format string, v ...interface{}) {
a.Writef(false, log.DEBUG, tag, format, v...)
}
func (a *ALog) DebugC(v ...interface{}) {
a.Write(true, log.DEBUG, "", v...)
}
func (a *ALog) DebugCf(format string, v ...interface{}) {
a.Writef(true, log.DEBUG, "", format, v...)
}
func (a *ALog) DebugTC(tag string, v ...interface{}) {
a.Write(true, log.DEBUG, tag, v...)
}
func (a *ALog) DebugTCf(tag string, format string, v ...interface{}) {
a.Writef(true, log.DEBUG, tag, format, v...)
}
func (a *ALog) Info(v ...interface{}) {
a.Write(false, log.INFO, "", v...)
}
func (a *ALog) Infof(format string, v ...interface{}) {
a.Writef(false, log.INFO, "", format, v...)
}
func (a *ALog) InfoT(tag string, v ...interface{}) {
a.Write(false, log.INFO, tag, v...)
}
func (a *ALog) InfoTf(tag string, format string, v ...interface{}) {
a.Writef(false, log.INFO, tag, format, v...)
}
func (a *ALog) InfoC(v ...interface{}) {
a.Write(true, log.INFO, "", v...)
}
func (a *ALog) InfoCf(format string, v ...interface{}) {
a.Writef(true, log.INFO, "", format, v...)
}
func (a *ALog) InfoTC(tag string, v ...interface{}) {
a.Write(true, log.INFO, tag, v...)
}
func (a *ALog) InfoTCf(tag string, format string, v ...interface{}) {
a.Writef(true, log.INFO, tag, format, v...)
}
func (a *ALog) Warn(v ...interface{}) {
a.Write(false, log.WARN, "", v...)
}
func (a *ALog) Warnf(format string, v ...interface{}) {
a.Writef(false, log.WARN, "", format, v...)
}
func (a *ALog) WarnT(tag string, v ...interface{}) {
a.Write(false, log.WARN, tag, v...)
}
func (a *ALog) WarnTf(tag string, format string, v ...interface{}) {
a.Writef(false, log.WARN, tag, format, v...)
}
func (a *ALog) WarnC(v ...interface{}) {
a.Write(true, log.WARN, "", v...)
}
func (a *ALog) WarnCf(format string, v ...interface{}) {
a.Writef(true, log.WARN, "", format, v...)
}
func (a *ALog) WarnTC(tag string, v ...interface{}) {
a.Write(true, log.WARN, tag, v...)
}
func (a *ALog) WarnTCf(tag string, format string, v ...interface{}) {
a.Writef(true, log.WARN, tag, format, v...)
}
func (a *ALog) Error(v ...interface{}) {
a.Write(false, log.ERROR, "", v...)
}
func (a *ALog) Errorf(format string, v ...interface{}) {
a.Writef(false, log.ERROR, "", format, v...)
}
func (a *ALog) ErrorT(tag string, v ...interface{}) {
a.Write(false, log.ERROR, tag, v...)
}
func (a *ALog) ErrorTf(tag string, format string, v ...interface{}) {
a.Writef(false, log.ERROR, tag, format, v...)
}
func (a *ALog) ErrorC(v ...interface{}) {
a.Write(true, log.ERROR, "", v...)
}
func (a *ALog) ErrorCf(format string, v ...interface{}) {
a.Writef(true, log.ERROR, "", format, v...)
}
func (a *ALog) ErrorTC(tag string, v ...interface{}) {
a.Write(true, log.ERROR, tag, v...)
}
func (a *ALog) ErrorTCf(tag string, format string, v ...interface{}) {
a.Writef(true, log.ERROR, tag, format, v...)
}
func (a *ALog) Fatal(v ...interface{}) {
a.Write(false, log.FATAL, "", v...)
}
func (a *ALog) Fatalf(format string, v ...interface{}) {
a.Writef(false, log.FATAL, "", format, v...)
}
func (a *ALog) FatalT(tag string, v ...interface{}) {
a.Write(false, log.FATAL, tag, v...)
}
func (a *ALog) FatalTf(tag string, format string, v ...interface{}) {
a.Writef(false, log.FATAL, tag, format, v...)
}
func (a *ALog) FatalC(v ...interface{}) {
a.Write(true, log.FATAL, "", v...)
}
func (a *ALog) FatalCf(format string, v ...interface{}) {
a.Writef(true, log.FATAL, "", format, v...)
}
func (a *ALog) FatalTC(tag string, v ...interface{}) {
a.Write(true, log.FATAL, tag, v...)
}
func (a *ALog) FatalTCf(tag string, format string, v ...interface{}) {
a.Writef(true, log.FATAL, tag, format, v...)
}