-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger.go
48 lines (39 loc) · 1.05 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
package logrusKit
import (
"github.com/richelieu-yang/chimera/v3/src/core/ioKit"
"github.com/richelieu-yang/chimera/v3/src/core/strKit"
"github.com/sirupsen/logrus"
)
// NewLogger
/*
PS:
(1) 默认输出到 控制台(os.Stderr);
(2) 如果希望 输出到文件(可选是否rotatable),可以使用 WithOutput(),详见下例.
@param options 可以什么都不配置(此时输出到控制台)
*/
func NewLogger(options ...LoggerOption) *logrus.Logger {
opts := loadOptions(options...)
logger := logrus.New()
logger.SetFormatter(opts.formatter)
logger.SetReportCaller(opts.reportCaller)
logger.SetLevel(opts.level)
if opts.output != nil {
logger.SetOutput(opts.output)
}
// msgPrefix
if strKit.IsNotEmpty(opts.msgPrefix) {
hook := &prefixHook{prefix: opts.msgPrefix}
logger.AddHook(hook)
}
if opts.disableQuote {
DisableQuote(logger)
}
return logger
}
// DisposeLogger 释放资源(主要针对文件日志)
func DisposeLogger(logger *logrus.Logger) error {
if logger == nil {
return nil
}
return ioKit.TryToClose(logger.Out)
}