/
log.go
157 lines (139 loc) · 3.19 KB
/
log.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
package modules
import (
"bufio"
"fmt"
"github.com/TISUnion/most-simple-mcd/constant"
_interface "github.com/TISUnion/most-simple-mcd/interface"
"github.com/TISUnion/most-simple-mcd/utils"
"os"
"path"
"path/filepath"
"strings"
"sync"
"time"
)
type Log struct {
Name string
Path string
Id int
Level int
WriteChan chan *_interface.LogMsgType
FileObj *os.File
lock *sync.Mutex // 文件锁:用于防止重新载入*os.file时,正在写入日志
}
// 直接写入日志,不进行格式化
func (l *Log) Write(p []byte) (n int, err error) {
l.WriteLog(&_interface.LogMsgType{
Message: string(p),
Level: constant.LOG_INFO,
IsNotFormat: true,
})
return 0, nil
}
func (l *Log) Errorf(f string, v ...interface{}) {
l.WriteLog(&_interface.LogMsgType{
Message: fmt.Sprintf(f, v),
Level: constant.LOG_ERROR,
})
}
func (l *Log) Warningf(f string, v ...interface{}) {
l.WriteLog(&_interface.LogMsgType{
Message: fmt.Sprintf(f, v),
Level: constant.LOG_WARNING,
})
}
func (l *Log) Infof(f string, v ...interface{}) {
l.WriteLog(&_interface.LogMsgType{
Message: fmt.Sprintf(f, v),
Level: constant.LOG_INFO,
})
}
func (l *Log) Debugf(f string, v ...interface{}) {
l.WriteLog(&_interface.LogMsgType{
Message: fmt.Sprintf(f, v),
Level: constant.LOG_DEBUG,
})
}
func (l *Log) ChangeConfCallBack() {}
func (l *Log) DestructCallBack() {
l.FileObj.Close()
}
func (l *Log) InitCallBack() {
}
func (l *Log) WriteLog(logMsg *_interface.LogMsgType) {
if l.Level > LogLevel[logMsg.Level] {
return
}
l.WriteChan <- logMsg
}
func (l *Log) writeToFile() {
for {
select {
case msg := <-l.WriteChan:
l.lock.Lock()
var data string
// 如果是否不需要格式化
if msg.IsNotFormat {
data = msg.Message
} else {
data = l.getLogMsg(msg.Level, msg.Message)
}
_, _ = l.FileObj.WriteString(data)
l.lock.Unlock()
}
}
}
func (l *Log) SetLogLevel(level string) {
if le, ok := LogLevel[level]; ok {
l.Level = le
}
}
func (l *Log) GetLines(page int, pageSize int) []string {
result := make([]string, 0)
scanner := bufio.NewScanner(l.FileObj)
start := (page - 1) * pageSize
end := page * pageSize
n := 0
for scanner.Scan() {
n++
if n > start && n < end {
result = append(result, scanner.Text())
}
}
return result
}
func (l *Log) CompressLogs(tpath string) error {
if tpath == "" {
extname := path.Ext(l.Path)
dir, oldFilename := filepath.Split(l.Path)
newFilename := strings.ReplaceAll(oldFilename, extname, ".zip")
tpath = dir + newFilename
}
if err := utils.CompressFile(l.Path, tpath); err != nil {
return err
}
return nil
}
func (l *Log) getLogMsg(level string, msg string) string {
timeStr := time.Now().Format(constant.LOG_TIME_FORMAT)
logMsg := fmt.Sprintf(constant.LOG_FORMAT, timeStr, level, msg)
return logMsg
}
func (l *Log) InitFileObj() {
l.lock.Lock()
defer l.lock.Unlock()
if l.FileObj != nil {
l.FileObj.Close()
}
var err error
// 文件不存在则新建
if l.FileObj, err = utils.CreateFile(l.Path); err != nil {
utils.PanicError(constant.CREATE_LOG_FAILED, err)
}
}
func (l *Log) Init() error {
l.lock = &sync.Mutex{}
l.InitFileObj()
go l.writeToFile()
return nil
}