-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
224 lines (189 loc) · 5.09 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
// @author wlanxww ([email protected])
// @version 1.0.0
package gologger
import (
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
)
type LogFile struct {
console *log.Logger
file *log.Logger
filename string
logFile *os.File
debugMode bool
withFile bool
lock *sync.Mutex
initialized bool
}
var logger LogFile
var OsExit = os.Exit
type messageLevel = int
const (
infoLevel messageLevel = iota
warnLevel
errorLevel
fatalLevel
panicLevel
debugLevel
)
var prefix = []string{"[info]", "[warn]", "[error]", "[fatal]", "[panic]", "[debug]"}
func (l *LogFile) output(level messageLevel, message string) {
if !l.initialized {
return
}
l.lock.Lock()
defer l.lock.Unlock()
if !l.debugMode && level == debugLevel {
return
}
prefix := fmt.Sprintf("%s [%s] ", prefix[level], getCallerPosition())
formatted := prefix + message
l.console.Output(3, formatted)
if l.withFile {
l.file.Output(3, formatted)
}
}
// SwitchExit is used to switch the program exit function.
// The `exit` parameter is a function that accepts an integer argument and replaces the standard os.Exit function.
//
// Note: This function is intended to be used only in a testing environment and should not be used in non-testing environments.
// In a testing environment, SwitchExit can be used to replace the program exit function with a custom function to capture the exit behavior during tests.
// Make sure that the custom exit function follows the behavior conventions of os.Exit, where the status code should be within the range [0, 125].
// It is not recommended to use this function to replace the os.Exit function in non-testing environments to avoid unnecessary side effects and erroneous behavior.
func SwitchExit(exit func(int)) {
OsExit = exit
}
// Init File logger
//
// @param debugMde bool enable debug log output
// @param withFile bool enable log file generation
// @param logFileName string custom log file, disabled when value is "" - empty string
//
// The default filename is the current date, and subsequent logs will be appended to the file.
// Custom filename functionality may be added in the future.
func InitFileLoger(debugMode bool, withFile bool, logFileName string) {
var err error
logger.debugMode = debugMode
logger.withFile = withFile
logger.lock = &sync.Mutex{}
logger.console = log.New(os.Stderr, "", log.LstdFlags)
if logger.withFile {
var file string
if logFileName == "" {
file = "./" + time.Now().Format("2006-01-02") + ".log"
} else {
file = logFileName
}
logger.filename = file
logger.logFile, err = os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatal("logfile init failed")
}
logger.file = log.New(logger.logFile, "", log.LstdFlags)
} else {
logger.filename = ""
}
logger.initialized = true
}
// Get log file name if enabled
//
// @returns string
func GetFileName() string {
return logger.filename
}
// Close log file
func CloseLogFile() {
if !logger.initialized {
return
}
logger.lock.Lock()
defer logger.lock.Unlock()
logger.logFile.Close()
}
func getCallerPosition() string {
pc, file, line, ok := runtime.Caller(3)
if !ok {
return "???"
}
funcName := runtime.FuncForPC(pc).Name()
if idx := strings.LastIndex(funcName, "/"); idx >= 0 {
funcName = funcName[idx+1:]
}
file = filepath.Base(file)
return fmt.Sprintf("%s:%d %s", file, line, funcName)
}
func Info(v ...interface{}) {
logger.output(infoLevel, fmt.Sprint(v...))
}
func Infof(format string, v ...interface{}) {
logger.output(infoLevel, fmt.Sprintf(format, v...))
}
func Infoln(v ...interface{}) {
logger.output(infoLevel, fmt.Sprintln(v...))
}
func Warn(v ...interface{}) {
logger.output(warnLevel, fmt.Sprint(v...))
}
func Warnf(format string, v ...interface{}) {
logger.output(warnLevel, fmt.Sprintf(format, v...))
}
func Warnln(v ...interface{}) {
logger.output(warnLevel, fmt.Sprintln(v...))
}
func Error(v ...interface{}) {
logger.output(errorLevel, fmt.Sprint(v...))
}
func Errorf(format string, v ...interface{}) {
logger.output(errorLevel, fmt.Sprintf(format, v...))
}
func Errorln(v ...interface{}) {
logger.output(errorLevel, fmt.Sprintln(v...))
}
func Fatal(v ...interface{}) {
logger.output(fatalLevel, fmt.Sprint(v...))
CloseLogFile()
OsExit(1)
}
func Fatalf(format string, v ...interface{}) {
logger.output(fatalLevel, fmt.Sprintf(format, v...))
CloseLogFile()
OsExit(1)
}
func Fatalln(v ...interface{}) {
logger.output(fatalLevel, fmt.Sprintln(v...))
CloseLogFile()
OsExit(1)
}
func Panic(v ...interface{}) {
s := fmt.Sprint(v...)
logger.output(panicLevel, s)
CloseLogFile()
panic(s)
}
func Panicf(format string, v ...interface{}) {
s := fmt.Sprintf(format, v...)
logger.output(panicLevel, s)
CloseLogFile()
panic(s)
}
func Panicln(v ...interface{}) {
s := fmt.Sprintln(v...)
logger.output(panicLevel, s)
CloseLogFile()
panic(s)
}
func Debug(v ...interface{}) {
logger.output(debugLevel, fmt.Sprint(v...))
}
func Debugf(format string, v ...interface{}) {
logger.output(debugLevel, fmt.Sprintf(format, v...))
}
func Debugln(v ...interface{}) {
logger.output(debugLevel, fmt.Sprintln(v...))
}