-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
74 lines (58 loc) · 1.61 KB
/
logging.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
/*
Copyright © 2023 Patrick Hermann [email protected]
*/
package base
import (
"os"
"time"
"github.com/mattn/go-colorable"
"github.com/sirupsen/logrus"
"github.com/snowzach/rotatefilehook"
)
type Logger struct{ *logrus.Logger }
func (l *Logger) Say(msg string) {
l.Info(msg)
}
func (l *Logger) Sayf(fmt string, args ...interface{}) {
l.Infof(fmt, args...)
}
func (l *Logger) SayWithField(msg string, k string, v map[string]string) {
l.WithField(k, v).Info(msg)
}
func (l *Logger) SayWithFields(msg string, fields map[string]interface{}) {
l.WithFields(fields).Info(msg)
}
func StdOutFileLogger(outputFileName, timeStampFormat string, maxSize, maxBackups, maxAge int) *Logger {
logLevel := logrus.InfoLevel
log := logrus.New()
log.SetLevel(logLevel)
rotateFileHook, err := rotatefilehook.NewRotateFileHook(rotatefilehook.RotateFileConfig{
Filename: outputFileName,
MaxSize: maxSize, // megabytes
MaxBackups: maxBackups, // amouts
MaxAge: maxAge, //days
Level: logLevel,
Formatter: &logrus.JSONFormatter{
TimestampFormat: time.RFC822,
},
})
if err != nil {
logrus.Fatalf("Failed to initialize file rotate hook: %v", err)
}
log.SetOutput(colorable.NewColorableStdout())
log.SetFormatter(&logrus.TextFormatter{
PadLevelText: true,
ForceColors: true,
FullTimestamp: true,
TimestampFormat: timeStampFormat,
})
log.AddHook(rotateFileHook)
return &Logger{log}
}
func CreateFileLogger(filepath string) (filewWiter *os.File) {
filewWiter, err := os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
panic(err)
}
return
}