-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
190 lines (162 loc) · 4.19 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
package log
import (
"fmt"
"github.com/op/go-logging"
"github.com/poolqa/log/rotator"
"log"
"os"
"path/filepath"
"runtime/debug"
"sync"
)
//var LogFilePath = "./log.conf" // your can change the file path before init
var _logger *logging.Logger
var once sync.Once
var _cfg *LogConfig
func init() {
_logger = logging.MustGetLogger("")
_logger.ExtraCalldepth = 1
}
func Default() {
var err error
_cfg, err = LoadLogConfigJson(GetDefaultLogConfig())
if err != nil {
debug.PrintStack()
log.Fatal(err)
}
once.Do(initLogger)
}
func InitByConfigFile(filePath string) {
var err error
_cfg, err = LoadLogConfigFile(filePath)
if err != nil {
debug.PrintStack()
log.Fatal(err)
}
once.Do(initLogger)
}
func InitByConfigJson(configJson string) {
var err error
_cfg, err = LoadLogConfigJson([]byte(configJson))
if err != nil {
debug.PrintStack()
log.Fatal(err)
}
once.Do(initLogger)
}
func InitByConfigStruct(conf *LogConfig) {
_cfg = conf
once.Do(initLogger)
}
func initLogDir(logFile string) error {
dir := filepath.Dir(logFile)
_, err := os.Stat(dir) //os.Stat获取文件信息
if err != nil {
if os.IsExist(err) {
return nil
}
return os.MkdirAll(dir, os.ModePerm)
}
return nil
}
func initLogger() {
_logger = logging.MustGetLogger("")
_logger.ExtraCalldepth = 1
// check log dir path
err := initLogDir(_cfg.FileName)
if err != nil {
log.Fatal(err)
}
logLevel := getLogLevel(_cfg.Level)
//logging.SetLevel(logLevel, "")
backendArr := []logging.Backend{}
format := logging.MustStringFormatter(_cfg.Format)
if _cfg.HasConsole {
backendConsole := logging.NewLogBackend(os.Stdout, "", 0)
backendConsole.Color = _cfg.Color
backendConsoleFormatter := logging.NewBackendFormatter(backendConsole, format)
backendConsoleLvLeveled := logging.AddModuleLevel(backendConsoleFormatter)
backendConsoleLvLeveled.SetLevel(logLevel, "")
backendArr = append(backendArr, backendConsoleLvLeveled)
}
rotateMode := getLogRotateMode(_cfg.DateSlice)
lf := rotator.NewLogger(_cfg.FileName, _cfg.MaxSize, _cfg.MaxAge, rotateMode, false)
backendNormal := logging.NewLogBackend(lf, "", 0)
backendNormalFormatter := logging.NewBackendFormatter(backendNormal, format)
backendNormalLvLeveled := logging.AddModuleLevel(backendNormalFormatter)
backendNormalLvLeveled.SetLevel(logLevel, "")
backendArr = append(backendArr, backendNormalLvLeveled)
if _cfg.LevelFileName != nil && len(_cfg.LevelFileName) > 0 {
for k, fp := range _cfg.LevelFileName {
lvFileLevel := getLogLevel(k)
lvF := rotator.NewLogger(fp, _cfg.MaxSize, _cfg.MaxAge, rotateMode, false)
backendLv := logging.NewLogBackend(lvF, "", 0)
backendLvFormatter := logging.NewBackendFormatter(backendLv, format)
backendLvLeveled := logging.AddModuleLevel(backendLvFormatter)
backendLvLeveled.SetLevel(lvFileLevel, "")
backendArr = append(backendArr, backendLvLeveled)
}
}
// Set the backends to be used.
logging.SetBackend(backendArr...)
}
//log critical level
func Critical(a ...interface{}) {
_logger.Critical(a...)
}
//log critical format
func Criticalf(format string, a ...interface{}) {
_logger.Criticalf(format, a...)
}
//log critical level
func Fatal(a ...interface{}) {
_logger.Critical(a...)
}
//log critical format
func Fatalf(format string, a ...interface{}) {
_logger.Criticalf(format, a...)
}
//log error level
func Error(a ...interface{}) {
_logger.Error(a...)
}
//log error format
func Errorf(format string, a ...interface{}) {
_logger.Errorf(format, a...)
}
//log warning level
func Warn(a ...interface{}) {
_logger.Warning(a...)
}
//log warning format
func Warnf(format string, a ...interface{}) {
_logger.Warningf(format, a...)
}
//log notice level
func Notice(a ...interface{}) {
_logger.Notice(a...)
}
//log notice format
func Noticef(format string, a ...interface{}) {
_logger.Noticef(format, a...)
}
//log info level
func Info(a ...interface{}) {
_logger.Info(a...)
}
//log info format
func Infof(format string, a ...interface{}) {
_logger.Infof(format, a...)
}
//log debug level
func Debug(a ...interface{}) {
_logger.Debug(a...)
}
//log debug format
func Debugf(format string, a ...interface{}) {
_logger.Debugf(format, a...)
}
func printError(message string) {
fmt.Println(message)
os.Exit(0)
}