-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolog.go
339 lines (272 loc) · 8.27 KB
/
golog.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
338
339
package golog
import (
"archive/zip"
"errors"
"fmt"
"io"
"os"
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/enescakir/emoji"
"github.com/fatih/color"
)
func New() *logger {
_, file, _, _ := runtime.Caller(1)
caller_working_dir := filepath.Dir(file)
logger := logger{
logger_config: logger_config{
datetime_format: "Mon, 02 Jan, 2006 15:04:05",
log_format: "%(asctime) [%(levelname)] - [%(filename).%(lineno)]: %(message)",
log_level: LOG_LEVEL_INFO,
log_stream: LOG_STREAM_FILE,
with_emoji: false,
exit_on_critical: false,
log_rotation_config: log_rotation_config{
file_name: filepath.Join(caller_working_dir, "access.log"),
max_file_size: 50 * 1024 * 1024,
max_rotation_days: 7,
rotate_file: true,
zip_archive: true,
},
},
}
return &logger
}
func (l *logger) generate_log(log_level string, msg string, caller_file string, caller_line int, emoji emoji.Emoji) string {
var log string
log = l.logger_config.log_format
now := time.Now().Format(l.logger_config.datetime_format)
log = strings.ReplaceAll(log, "%(asctime)", now)
log = strings.ReplaceAll(log, "%(levelname)", log_level)
log = strings.ReplaceAll(log, "%(filename)", caller_file)
log = strings.ReplaceAll(log, "%(lineno)", strconv.Itoa(caller_line))
log = strings.ReplaceAll(log, "%(message)", msg)
if l.logger_config.with_emoji {
log = strings.Replace(log, "-", emoji.String(), 1)
}
return log
}
func (l *logger) compress_file() error {
file_path := l.logger_config.log_rotation_config.file_name
log_file, err := os.Open(file_path)
if err != nil {
color.Red("Can't access the log file for rotation.\n")
return err
}
defer log_file.Close()
var dir_slice []string
if runtime.GOOS == "windows" {
dir_slice = strings.Split(file_path, "\\")
} else {
dir_slice = strings.Split(file_path, "/")
}
log_file_name := dir_slice[len(dir_slice)-1]
log_dir := filepath.Dir(file_path)
file_name := filepath.Base(file_path)
log_zip, err := os.Create(path.Join(log_dir, fmt.Sprintf("%s.zip", file_name)))
if err != nil {
color.Red("An error occured while creating zip file: %s\n", err)
return err
}
defer log_zip.Close()
zip_writer := zip.NewWriter(log_zip)
file_info, err := log_file.Stat()
if err != nil {
color.Red("An error occured while retrieving file info: %s\n", err)
return err
}
header, err := zip.FileInfoHeader(file_info)
if err != nil {
color.Red("An error occured while creating file header: %s\n", err)
return err
}
header.Name = log_file_name
writer, err := zip_writer.CreateHeader(header)
if err != nil {
color.Red("An error occured while creating file in zip archive: %s\n", err)
return err
}
_, err = io.Copy(writer, log_file)
if err != nil {
color.Red("An error occured while copying file to zip archive: %s\n", err)
return err
}
err = zip_writer.Close()
if err != nil {
color.Red("An error occured while closing zip file: %s\n", err)
return err
}
color.Magenta("Log file [%s] has successfully been archived.\n", log_file_name)
return nil
}
func (l *logger) rotate_file(file *os.File, file_name string, roatate_msg string) error {
color.Magenta("Rotating old log file, %s.\n", roatate_msg)
l.compress_file()
file.Close()
err := os.Remove(l.logger_config.log_rotation_config.file_name)
if err != nil {
color.Red("Could not remove old log file.\n")
return err
}
file, err = os.OpenFile(file_name, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
color.Red("An error occured while creating new log file.\n")
return err
}
defer file.Close()
return nil
}
func (l *logger) write_file(log string) error {
file_path := l.logger_config.log_rotation_config.file_name
if file_path == "" {
color.Red("File name not specified for logging.\n")
return errors.New("file name not specified for logging")
}
file, err := os.OpenFile(file_path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
color.Red("An error occured while accessing the log file.\n")
return err
}
defer file.Close()
file_stat, err := file.Stat()
if err != nil {
file.Close()
color.Red("An error occured while retrieving file stats.\n")
return err
}
size := file_stat.Size()
if size >= l.logger_config.log_rotation_config.max_file_size {
err = l.rotate_file(file, file_path, "max size reached")
defer file.Close()
if err != nil {
return err
}
}
mod_time := file_stat.ModTime()
max_days := l.logger_config.log_rotation_config.max_rotation_days
if time.Since(mod_time) > time.Duration(max_days)*24*time.Hour {
err = l.rotate_file(file, file_path, "max days reached")
defer file.Close()
if err != nil {
return err
}
}
_, err = file.WriteString(fmt.Sprintf("%s\n", log))
return err
}
func (l *logger) Set_Log_Level(log_level LOG_LEVEL) {
l.logger_config.log_level = log_level
}
func (l *logger) Set_Log_Format(log_format string) {
l.logger_config.log_format = log_format
}
func (l *logger) Set_Log_Stream(log_stream LOG_STREAM) {
l.logger_config.log_stream = log_stream
}
func (l *logger) Set_Datetime_Format(format string) {
l.logger_config.datetime_format = format
}
func (l *logger) Set_File_Name(file_name string) {
l.logger_config.log_rotation_config.file_name = file_name
}
func (l *logger) Set_Max_File_Size(max_size int64) {
l.logger_config.log_rotation_config.max_file_size = max_size
}
func (l *logger) Set_Max_Days(max_days int) {
l.logger_config.log_rotation_config.max_rotation_days = max_days
}
func (l *logger) Zip_Archive(zip_archive bool) {
l.logger_config.log_rotation_config.zip_archive = zip_archive
}
func (l *logger) With_Emoji(with_emoji bool) {
l.logger_config.with_emoji = with_emoji
}
func (l *logger) Exit_On_Critical(exit bool) {
l.logger_config.exit_on_critical = exit
}
func get_log_level_string(log_level LOG_LEVEL) string {
switch log_level {
case LOG_LEVEL_TRACE:
return "TRACE"
case LOG_LEVEL_DEBUG:
return "DEBUG"
case LOG_LEVEL_INFO:
return "INFO"
case LOG_LEVEL_WARN:
return "WARN"
case LOG_LEVEL_ERROR:
return "ERROR"
case LOG_LEVEL_CRITICAL:
return "CRITICAL"
default:
return ""
}
}
func (l *logger) log(level LOG_LEVEL, msg string, levelColor func(string, ...interface{}), emoji emoji.Emoji) error {
_, file, line, _ := runtime.Caller(2)
file = filepath.Base(file)
log_level := get_log_level_string(level)
log := l.generate_log(log_level, msg, file, line, emoji)
if l.logger_config.log_level <= level {
if l.logger_config.log_stream == LOG_STREAM_CONSOLE || l.logger_config.log_stream == LOG_STREAM_MULTIPLE {
levelColor("%s\n", log)
}
if l.logger_config.log_stream == LOG_STREAM_FILE || l.logger_config.log_stream == LOG_STREAM_MULTIPLE {
err := l.write_file(log)
if err != nil {
return err
}
}
}
return nil
}
func (l *logger) TRACE(msg string, a ...interface{}) {
formattedMsg := fmt.Sprintf(msg, a...)
err := l.log(LOG_LEVEL_TRACE, formattedMsg, color.White, emoji.Unicorn)
if err != nil {
color.Red("Error while logging: %v\n", err)
}
}
func (l *logger) DEBUG(msg string, a ...interface{}) {
formattedMsg := fmt.Sprintf(msg, a...)
err := l.log(LOG_LEVEL_DEBUG, formattedMsg, color.Blue, emoji.VideoGame)
if err != nil {
color.Red("Error while logging: %v\n", err)
}
}
func (l *logger) INFO(msg string, a ...interface{}) {
formattedMsg := fmt.Sprintf(msg, a...)
err := l.log(LOG_LEVEL_INFO, formattedMsg, color.Magenta, emoji.LightBulb)
if err != nil {
color.Red("Error while logging: %v\n", err)
}
}
func (l *logger) WARN(msg string, a ...interface{}) {
formattedMsg := fmt.Sprintf(msg, a...)
err := l.log(LOG_LEVEL_WARN, formattedMsg, color.Yellow, emoji.Loudspeaker)
if err != nil {
color.Red("Error while logging: %v\n", err)
}
}
func (l *logger) ERROR(msg string, a ...interface{}) {
formattedMsg := fmt.Sprintf(msg, a...)
err := l.log(LOG_LEVEL_ERROR, formattedMsg, color.Red, emoji.CrossMark)
if err != nil {
color.Red("Error while logging: %v\n", err)
}
}
func (l *logger) CRITICAL(msg string, a ...interface{}) {
formattedMsg := fmt.Sprintf(msg, a...)
critical := color.New(color.FgRed, color.Bold, color.Underline)
err := l.log(LOG_LEVEL_CRITICAL, formattedMsg, critical.PrintfFunc(), emoji.CrossedFlags)
if err != nil {
color.Red("Error while logging: %v\n", err)
}
if l.logger_config.exit_on_critical {
os.Exit(1)
}
}