-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
221 lines (182 loc) · 4.81 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
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
package log
import (
"fmt"
"github.com/abeir/desktop-app/core"
"github.com/abeir/desktop-app/core/config"
"github.com/gookit/color"
"github.com/lestrrat-go/file-rotatelogs"
"github.com/pkg/errors"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
"os"
"path"
"time"
)
var log *logrus.Logger
func IsTraceEnabled() bool{
return log.GetLevel() <= logrus.TraceLevel
}
func IsDebugEnabled() bool{
return log.GetLevel() <= logrus.DebugLevel
}
func IsInfoEnabled() bool{
return log.GetLevel() <= logrus.InfoLevel
}
func IsWarnEnabled() bool{
return log.GetLevel() <= logrus.WarnLevel
}
func IsErrorEnabled() bool{
return log.GetLevel() <= logrus.ErrorLevel
}
func IsFatalEnabled() bool{
return log.GetLevel() <= logrus.FatalLevel
}
func IsPanicEnabled() bool{
return log.GetLevel() <= logrus.PanicLevel
}
func Print(args ...interface{}) {
log.Print(args...)
}
func Printf(format string, args ...interface{}) {
log.Printf(format, args...)
}
func Println(args ...interface{}) {
log.Println(args...)
}
func Trace(args ...interface{}) {
log.Trace(args...)
}
func Tracef(format string, args ...interface{}) {
log.Tracef(format, args...)
}
func Traceln(args ...interface{}) {
log.Traceln(args...)
}
func Debug(args ...interface{}) {
log.Debug(args...)
}
func Debugf(format string, args ...interface{}) {
log.Debugf(format, args...)
}
func Debugln(args ...interface{}) {
log.Debugln(args...)
}
func Info(args ...interface{}) {
log.Info(args...)
}
func Infof(format string, args ...interface{}) {
log.Infof(format, args...)
}
func Infoln(args ...interface{}) {
log.Infoln(args...)
}
func Warn(args ...interface{}) {
log.Warn(args...)
}
func Warnf(format string, args ...interface{}) {
log.Warnf(format, args...)
}
func Warnln(args ...interface{}) {
log.Warnln(args...)
}
func Error(args ...interface{}) {
log.Error(args...)
}
func Errorf(format string, args ...interface{}) {
log.Errorf(format, args...)
}
func Errorln(args ...interface{}) {
log.Errorln(args...)
}
func Fatal(args ...interface{}) {
log.Fatal(args...)
}
func Fatalf(format string, args ...interface{}) {
log.Fatalf(format, args...)
}
func Fatalln(args ...interface{}) {
log.Fatalln(args...)
}
func Panic(args ...interface{}) {
log.Panic(args...)
}
func Panicf(format string, args ...interface{}) {
log.Panicf(format, args...)
}
func Panicln(args ...interface{}) {
log.Panicln(args...)
}
// InitLog 初始化日志,初始化失败时会抛出异常
// param
// config: 系统配置
func InitLog(config *config.ApplicationConfig){
if log!=nil {
log.Warn("日志系统已初始化,请不要重复调用InitLog")
return
}
var maxAgeDuration time.Duration
var rotationTimeDuration time.Duration
maxAge := config.Logger.MaxAge
if maxAge=="" {
maxAgeDuration = time.Hour * 24 * 60 //60天
}else{
duration, err := time.ParseDuration(maxAge)
if err!=nil {
panic(fmt.Sprintf("init log maxAge fail: %s, %s", maxAge, err.Error()))
}
maxAgeDuration = duration
}
rotationTime := config.Logger.RotationTime
if rotationTime=="" {
rotationTimeDuration = time.Hour * 24 //1天
}else{
duration, err := time.ParseDuration(rotationTime)
if err!=nil {
panic(fmt.Sprintf("init log rotationTime fail: %s, %s", rotationTime, err.Error()))
}
rotationTimeDuration = duration
}
configLocalFilesystemLogger(config.Logger.Level,
config.Logger.Path,
config.Logger.Filename,
maxAgeDuration,
rotationTimeDuration)
}
// config logrus log to local filesystem, with file rotation
func configLocalFilesystemLogger(level string, logPath string, logFileName string,
maxAge time.Duration, rotationTime time.Duration) {
color.Printf("<light_green>ready to init log:</> level:%s, logPath:%s, logFilename:%s, maxAge:%s, rotationTime:%s \n",
level, logPath, logFileName, maxAge.String(), rotationTime.String())
if !core.IsExists(logPath) {
err := os.MkdirAll(logPath, os.ModePerm)
if err!=nil {
panic(err)
}
}
log = logrus.New()
lv, err := logrus.ParseLevel(level)
if err!=nil {
log.Panicf("cannot parse log level: %s, %+v", level, errors.WithStack(err))
}
log.SetLevel(lv)
baseLogPath := path.Join(logPath, logFileName)
writer, err := rotatelogs.New(
baseLogPath+".%Y%m%d%H%M",
rotatelogs.WithLinkName(baseLogPath), // 生成软链,指向最新日志文件
rotatelogs.WithMaxAge(maxAge), // 文件最大保存时间
rotatelogs.WithRotationTime(rotationTime), // 日志切割时间间隔
)
if err != nil {
log.Panicf("config local file system logger error. %+v", errors.WithStack(err))
}
lfHook := lfshook.NewHook(lfshook.WriterMap{
logrus.TraceLevel: writer,
logrus.DebugLevel: writer, // 为不同级别设置不同的输出目的
logrus.InfoLevel: writer,
logrus.WarnLevel: writer,
logrus.ErrorLevel: writer,
logrus.FatalLevel: writer,
logrus.PanicLevel: writer,
}, &logrus.JSONFormatter{})
log.AddHook(lfHook)
}