-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
191 lines (167 loc) · 4.67 KB
/
logger.go
File metadata and controls
191 lines (167 loc) · 4.67 KB
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
package hamgo
import (
"bytes"
"fmt"
"os"
"os/signal"
"runtime"
"strings"
"sync"
"time"
)
//Log : use Log to write logs
var Log logger
type logger interface {
Error(format string, a ...interface{})
Info(format string, a ...interface{})
Debug(format string, a ...interface{})
Warn(format string, a ...interface{})
}
type fileLogger struct {
FilePath string
fileFolder string
fileMaxSize int64
fileSize int64
Format string
buf *bytes.Buffer
mutex *sync.Mutex
bufTime time.Duration
bufSize int
console bool
}
const (
defaultFilePath = "./app.log"
defaultWriteBufTime = 30 //s
defaultWriteBufSize = 1 * 1024 //KB
defaultFileMaxSize = 1024 * 1024 //MB
defaultConsole = true
defaultFormat = "[%Title] [%Time] [%File] %Text"
logTitleDebug = "Debug"
logTitleInfo = "Info"
logTitleError = "Error"
logTitleWarn = "Warn"
confFilePath = "log_file"
confFileMaxSize = "log_file_max_size"
confBufSize = "log_buf_size"
confBufTime = "log_buf_time"
confConsole = "log_console"
confFormat = "log_format"
confFormatTitle = "%Title"
confFormatFile = "%File"
confFormatTime = "%Time"
confFormatText = "%Text"
)
func setLog(filePath string) {
var fileLog *fileLogger
//set log by conf
if Conf != nil {
fileLog = &fileLogger{
FilePath: Conf.DefaultString(confFilePath, defaultFilePath),
fileFolder: currentPath(Conf.DefaultString(confFilePath, defaultFilePath)),
Format: Conf.DefaultString(confFormat, defaultFormat),
console: Conf.DefaultBool(confConsole, defaultConsole),
buf: new(bytes.Buffer),
mutex: new(sync.Mutex),
fileMaxSize: Conf.DefaultInt64(confFileMaxSize, defaultFileMaxSize) * 1024,
fileSize: 0,
bufTime: time.Second * time.Duration(Conf.DefaultInt64(confBufTime, defaultWriteBufTime)),
bufSize: Conf.DefaultInt(confBufSize, defaultWriteBufSize) * 1024}
} else {
if filePath == "" {
filePath = defaultFilePath
}
//set by manual
fileLog = &fileLogger{
FilePath: filePath,
fileFolder: currentPath(filePath),
Format: defaultFormat,
console: true,
buf: new(bytes.Buffer),
mutex: new(sync.Mutex),
fileMaxSize: defaultFileMaxSize,
fileSize: 0,
bufTime: time.Second * time.Duration(defaultWriteBufTime),
bufSize: defaultWriteBufSize}
}
//create a thread to write buf to log file
go fileLog.writeBuf()
//listen exit signal
go fileLog.onExit()
Log = fileLog
}
func (log *fileLogger) Error(format string, a ...interface{}) {
log.writeAndPrint(logTitleError, format, a...)
}
func (log *fileLogger) Info(format string, a ...interface{}) {
log.writeAndPrint(logTitleInfo, format, a...)
}
func (log *fileLogger) Debug(format string, a ...interface{}) {
log.writeAndPrint(logTitleDebug, format, a...)
}
func (log *fileLogger) Warn(format string, a ...interface{}) {
log.writeAndPrint(logTitleWarn, format, a...)
}
func (log *fileLogger) writeBuf() {
for {
//1.sleep
time.Sleep(log.bufTime)
//2.check file
log.checkFile()
//3.check bufsize
if log.buf.Len() < log.bufSize {
continue
}
//4.write
log.mutex.Lock()
len := int64(log.buf.Len())
if writeBytes(log.FilePath, log.buf.Bytes()) {
log.buf.Reset()
//5.add size
log.fileSize = log.fileSize + len
}
log.mutex.Unlock()
}
}
func (log *fileLogger) writeAndPrint(title, format string, a ...interface{}) {
line := log.format(title, fmt.Sprintf(format, a...))
if log.console {
fmt.Print(line)
}
log.mutex.Lock()
log.buf.WriteString(line)
log.mutex.Unlock()
}
func (log *fileLogger) onExit() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
<-c
writeBytes(log.FilePath, log.buf.Bytes())
os.Exit(1)
}
func (log *fileLogger) checkFile() {
if log.fileSize > log.fileMaxSize {
stmp := time.Now().Format(log.fileFolder + "2006_01_02_15_04_05.log")
if renameFile(log.FilePath, stmp) {
openFile(log.FilePath).Close()
log.fileSize = 0
}
}
}
func (log *fileLogger) format(title, text string) string {
f := log.Format
if strings.Contains(f, confFormatTitle) {
f = strings.Replace(f, confFormatTitle, fmt.Sprintf("%-5s", title), -1)
}
if strings.Contains(f, confFormatFile) {
_, fileName, lineNum, _ := runtime.Caller(3)
f = strings.Replace(f, confFormatFile, fmt.Sprintf("%s:%d", fileName, lineNum), -1)
}
if strings.Contains(f, confFormatTime) {
stmp := time.Now().Format("2006/01/02/15:04:05")
f = strings.Replace(f, confFormatTime, stmp, -1)
}
if strings.Contains(f, confFormatText) {
f = strings.Replace(f, confFormatText, text, -1)
}
return fmt.Sprintf("%s\n", f)
}