-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcblogger.go
135 lines (113 loc) · 3.81 KB
/
cblogger.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
// CB-Log: Logger for Cloud-Barista.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// ref) https://github.com/sirupsen/logrus
// ref) https://github.com/natefinch/lumberjack
// ref) https://github.com/snowzach/rotatefilehook
// by CB-Log Team, 2019.08.
package cblog
import (
"os"
"time"
cblogformatter "github.com/cloud-barista/cb-log/formatter"
"github.com/sirupsen/logrus"
"github.com/snowzach/rotatefilehook"
)
type CBLogger struct {
loggerName string
logrus *logrus.Logger
}
// global var.
var (
thisLogger *CBLogger
thisFormatter *cblogformatter.Formatter
cblogConfig CBLOGCONFIG
)
// Get the logger with name you set. The name will be used as below (name: CB-SPIDER)
// [CB-SPIDER].[INFO]: 2020-12-24 16:54:46 sample-with-config-path.go:27, main.main() - start.........
// Read configuration file (log_conf.yaml) by the path set on environment variable (e.g., $CBLOG_ROOT)
func GetLogger(loggerName string) *logrus.Logger {
return getLoggerHandler(loggerName, "")
}
// Read configuration file (log_conf.yaml) from the path you set
func GetLoggerWithConfigPath(loggerName string, configFilePath string) *logrus.Logger {
return getLoggerHandler(loggerName, configFilePath)
}
// The handler for GetLogger() and GetLoggerWithConfigPath()
func getLoggerHandler(loggerName string, configFilePath string) *logrus.Logger {
if thisLogger != nil {
return thisLogger.logrus
}
thisLogger = new(CBLogger)
thisLogger.loggerName = loggerName
thisLogger.logrus = &logrus.Logger{
Level: logrus.DebugLevel,
Out: os.Stderr,
Hooks: make(logrus.LevelHooks),
Formatter: getFormatter(loggerName),
}
// set config.
setup(loggerName, configFilePath)
return thisLogger.logrus
}
func setup(loggerName string, configFilePath string) {
cblogConfig = GetConfigInfos(configFilePath)
thisLogger.logrus.SetReportCaller(true)
if cblogConfig.CBLOG.LOOPCHECK {
SetLevel(cblogConfig.CBLOG.LOGLEVEL)
go levelSetupLoop(loggerName, configFilePath)
} else {
SetLevel(cblogConfig.CBLOG.LOGLEVEL)
}
if cblogConfig.CBLOG.LOGFILE {
setRotateFileHook(loggerName, &cblogConfig)
}
}
// Now, this method is busy wait.
// @TODO must change this with file watch&event.
// ref) https://github.com/fsnotify/fsnotify/blob/master/example_test.go
func levelSetupLoop(loggerName string, configFilePath string) {
for {
cblogConfig = GetConfigInfos(configFilePath)
SetLevel(cblogConfig.CBLOG.LOGLEVEL)
time.Sleep(time.Second * 2)
}
}
func setRotateFileHook(loggerName string, logConfig *CBLOGCONFIG) {
level, _ := logrus.ParseLevel(logConfig.CBLOG.LOGLEVEL)
rotateFileHook, err := rotatefilehook.NewRotateFileHook(rotatefilehook.RotateFileConfig{
Filename: logConfig.LOGFILEINFO.FILENAME,
MaxSize: logConfig.LOGFILEINFO.MAXSIZE, // megabytes
MaxBackups: logConfig.LOGFILEINFO.MAXBACKUPS,
MaxAge: logConfig.LOGFILEINFO.MAXAGE, //days
Level: level,
Formatter: getFormatter(loggerName),
})
if err != nil {
logrus.Fatalf("Failed to initialize file rotate hook: %v", err)
}
thisLogger.logrus.AddHook(rotateFileHook)
}
func SetLevel(strLevel string) {
level, err := logrus.ParseLevel(strLevel)
if err != nil {
thisLogger.logrus.Warnf("Not available logging level: %v. Default logging level will be used: debug", strLevel)
level = logrus.DebugLevel
}
thisLogger.logrus.SetLevel(level)
}
func GetLevel() string {
return thisLogger.logrus.GetLevel().String()
}
func getFormatter(loggerName string) *cblogformatter.Formatter {
if thisFormatter != nil {
return thisFormatter
}
// 출력 포맷 조정 (keyvalues) 추가 (Formatter.go에서 해당 위치에 실제 데이터로 변경)
thisFormatter = &cblogformatter.Formatter{
TimestampFormat: "2006-01-02 15:04:05",
LogFormat: "[" + loggerName + "]." + "[%lvl%]: %time% %func% - %msg% \t[%keyvalues%]\n",
}
return thisFormatter
}