-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
337 lines (298 loc) · 6.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package QLog
import (
"fmt"
"io"
"net"
"os"
"strconv"
"strings"
"sync"
"time"
)
var (
YRDLogger LoggerInterface
ServerIp string
TimeLocation *time.Location
)
type LoggerInterface interface {
SetConfig(uint8, string, ...ConfigOption)
SetTextPrefix(...interface{})
AddTextPrefix(...interface{})
Debug(v ...interface{})
Info(v ...interface{})
Warn(v ...interface{})
Error(v ...interface{})
Fatal(v ...interface{})
AlertWithLevel(alertLevel string, v ...interface{})
}
type logger struct {
logLevel uint8 // 默认为0
isConsole bool // false
isFile bool // false
//文件相关配置
filePath string
filename string
fileSuffix string
fileMaxSize int64
fileMaxNSize int
fileCurrSize int64 //文件大小,字节
//日志前缀信息
textPrefix string
//报警配置
alertAppId string
alertURL string
alertContentType string
alertFormat string
//日期格式
dateFormat string
timeLocation *time.Location
callDep int
nSize int //超过设定文件大小的重命名文件序号
mu *sync.RWMutex
logfile *os.File
outFile io.Writer
outConsole io.Writer
_date *time.Time
}
func init() {
var (
addrs []net.Addr
err error
ips []string
)
addrs, err = net.InterfaceAddrs()
if err != nil {
} else {
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ips = append(ips, ipnet.IP.String())
}
}
}
}
ServerIp = strings.Join(ips, "/")
}
func GetLogger() LoggerInterface {
if YRDLogger == nil {
YRDLogger = &logger{mu: new(sync.RWMutex), nSize: 1, callDep: callDep}
}
return YRDLogger
}
func (l *logger) SetConfig(logLevel uint8, zone string, opts ...ConfigOption) {
var err error
TimeLocation, err = time.LoadLocation(zone) // 时区
if err != nil {
fmt.Println(err)
TimeLocation, _ = time.LoadLocation("Local") // 时区
}
t, _ := time.Parse(_DATEFORMAT, GetNowUnixTimeOBJ().Format(_DATEFORMAT)) //当日零点
l._date = &t
l.logLevel = logLevel
for _, optFunc := range opts {
optFunc(l)
}
l.setLogger()
}
type ConfigOption func(*logger)
func WithConsoleOPT() ConfigOption {
return func(l *logger) {
l.isConsole = true
}
}
func WithFileOPT(filepath, filename, filesuffix string, fileMaxSize int64, fileMaxNSize int) ConfigOption {
strDefault(&filename, DEFAULTFILENAME)
strDefault(&filepath, DEFAULTFILEPATH)
strDefault(&filesuffix, DEFAULTFILESUFFIX)
int64Default(&fileMaxSize, DEFAULTFILEMAXSIZE)
return func(l *logger) {
l.isFile = true
l.filePath = absolutePath(filepath)
l.filename = filename
l.fileSuffix = DOT + filesuffix
l.fileMaxSize = fileMaxSize
l.fileMaxNSize = fileMaxNSize
}
}
// 报警配置
func WithAlertOPT(appId, URL, ContentType string) ConfigOption {
return func(l *logger) {
l.alertAppId = appId
l.alertURL = URL
l.alertContentType = ContentType
}
}
func WithCommonOPT(cDep int, dateFormat string) ConfigOption {
return func(l *logger) {
l.callDep = cDep
l.dateFormat = dateFormat
}
}
func WithcallDepOPT(cDep int) ConfigOption {
return func(l *logger) {
if cDep == 0 {
l.callDep = callDep
} else {
l.callDep = cDep
}
}
}
func (l *logger) SetTextPrefix(keyvals ...interface{}) {
l.textPrefix = format(keyvals...)
}
func (l *logger) AddTextPrefix(keyvals ...interface{}) {
l.textPrefix += format(keyvals...)
}
func (l *logger) setLogger() {
if l.isConsole {
l.outConsole = os.Stdout
}
if l.isFile {
mkdirlog(l.filePath)
l.openFile()
}
}
func (l *logger) getFileFullName() string {
return l.filePath + "/" + l.filename + UNDERSCODE + l._date.Format(_DATEFORMAT) + l.fileSuffix
}
func (l *logger) getSizeFileFullName(num string) string {
return l.filePath + "/" + l.filename + UNDERSCODE + l._date.Format(_DATEFORMAT) + DOT + num + l.fileSuffix
}
func (l *logger) openFile() {
defer catchError()
var err error
l.logfile, err = os.OpenFile(l.getFileFullName(), os.O_RDWR|os.O_APPEND|os.O_CREATE, 0766)
if err != nil {
panic("不能打开/创建文件 " + err.Error())
}
l.outFile = l.logfile
fileInfo, err := l.logfile.Stat()
if err != nil {
panic("获取fileinfo出错")
}
l.fileCurrSize = fileInfo.Size()
}
func (l *logger) Debug(keyvals ...interface{}) {
l.log(TYPEDEBUG, DEBUG, "", keyvals...)
}
func (l *logger) Info(keyvals ...interface{}) {
l.log(TYPEINFO, INFO, "", keyvals...)
}
func (l *logger) Warn(keyvals ...interface{}) {
l.log(TYPEWARN, WARN, "", keyvals...)
}
func (l *logger) Error(keyvals ...interface{}) {
l.log(TYPEERROR, ERROR, "", keyvals...)
}
func (l *logger) Fatal(keyvals ...interface{}) {
l.log(TYPEFATAL, FATAL, "", keyvals...)
os.Exit(1)
}
func (l *logger) AlertWithLevel(alertLevel string, keyvals ...interface{}) {
l.log(TYPEALERT, ALERT, alertLevel, keyvals...)
}
func (l *logger) log(level string, _level uint8, alertLevel string, keyvals ...interface{}) {
defer catchError()
if l.logLevel <= _level {
s := GetLogTextPrefix(l.callDep+1, l.dateFormat) +
strings.TrimRight(fmt.Sprint(level, BLANK, "IP=", ServerIp, BLANK, l.textPrefix, format(keyvals...)), BLANK) +
NEWLINE
// 判断是否调用alert
if _level == ALERT {
go l.alert(s, alertLevel)
}
n := l.write(s)
if l.isFile {
l.fileCurrSize += int64(n)
l.fileCheck()
}
}
}
func (l *logger) write(v string) int {
l.mu.RLock()
defer func() {
l.mu.RUnlock()
catchError()
}()
var (
n int
err error
)
if l.isConsole {
l.outConsole.Write([]byte(v))
}
if l.isFile {
n, err = l.outFile.Write([]byte(v))
if err != nil {
panic("写文件出错")
}
}
return n
}
func (l *logger) fileCheck() {
defer catchError()
if l.isMustRenameDate() {
l.renameDate()
}
if l.isMustRenameSize() {
l.renameSize()
}
}
func (l *logger) isMustRenameSize() bool {
l.mu.Lock()
defer l.mu.Unlock()
if l.fileCurrSize >= l.fileMaxSize {
return true
}
return false
}
func (l *logger) isMustRenameDate() bool {
l.mu.Lock()
defer l.mu.Unlock()
t, _ := time.Parse(_DATEFORMAT, GetNowUnixTimeOBJ().Format(_DATEFORMAT))
if t.After(*l._date) {
l._date = &t
return true
}
return false
}
//按日期
func (l *logger) renameDate() {
defer catchError()
l.mu.Lock()
defer l.mu.Unlock()
l.close()
l.openFile()
}
//按大小
func (l *logger) renameSize() {
defer catchError()
l.mu.Lock()
defer l.mu.Unlock()
l.close()
// 检测此文件是否已经存在
num := 1
for isExists(l.getSizeFileFullName(strconv.Itoa(num))) {
//strconv.Itoa(l.nSize)
//l.nSize++
num++
}
// 删除大于fileMaxNSize的文件
for ; num > l.fileMaxNSize; num-- {
os.Remove(l.getSizeFileFullName(strconv.Itoa(num)))
}
// 重命名文件
for ; num > 1; num-- {
os.Rename(l.getSizeFileFullName(strconv.Itoa(num-1)), l.getSizeFileFullName(strconv.Itoa(num)))
}
os.Rename(l.getFileFullName(), l.getSizeFileFullName("1"))
l.openFile()
l.nSize++
l.flush()
}
func (l *logger) flush() {
l.fileCurrSize = 0
}
func (l *logger) close() {
l.logfile.Close()
}