-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
189 lines (165 loc) · 4.83 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
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
package log
import (
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"time"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/sirupsen/logrus"
)
// var logger *logrus.Entry
var logger = logrus.New()
const (
_path = "../LOG/record.log"
_infoPath = "info.log"
_errorPath = "error.log"
_rotationHour = 24
_maxFile = 30
_timeStampFormat = "2006-01-02-15:04:05"
)
type customFormatter struct{}
func (s *customFormatter) Format(entry *logrus.Entry) ([]byte, error) {
timestamp := time.Now().Local().Format(_timeStampFormat)
var levelColor int
switch entry.Level {
case logrus.DebugLevel:
levelColor = 36 // blue
case logrus.InfoLevel:
levelColor = 32 // green
case logrus.WarnLevel:
levelColor = 93 // yellow
case logrus.ErrorLevel:
levelColor = 31 // red
case logrus.FatalLevel:
levelColor = 41 // background red
case logrus.PanicLevel:
levelColor = 45 // background purple
default:
levelColor = 37
}
// var file string
// var line int
// if entry.Caller != nil {
// file = filepath.Base(entry.Caller.File)
// line = entry.Caller.Line
// }
// msg := fmt.Sprintf("%s [%s]%-1s[%s:%d] %1s\n", timestamp, strings.ToUpper(entry.Level.String()), "\t", file, line, entry.Message)
// msg := fmt.Sprintf("%s [%s]%-1s%s\n", timestamp, strings.ToUpper(entry.Level.String()), "\t", entry.Message)
msg := fmt.Sprintf("\x1b[%dm%s [%s] %s\n\x1b[0m", levelColor, timestamp, strings.ToUpper(entry.Level.String()), entry.Message)
return []byte(msg), nil
}
func init() {
// 日誌分割相關
// `WithLinkName` 建立日誌
// `WithRotationTime` 設定日誌分割的時間
// WithMaxAge 和 WithRotationCount只能設定一個
// `WithMaxAge` 設定檔案清理前最長儲存的時間
// `WithRotationCount` 設定檔案清理最多儲存的數量
// 下面配置每隔1天建立新檔案,保留最近30天的日誌檔案,其他的則清理掉
writerFile, _ := rotatelogs.New(
_path+".%Y%m%d",
rotatelogs.WithLinkName(_path),
// rotatelogs.WithMaxAge(time.Duration(180)*time.Second),
rotatelogs.WithRotationCount(_maxFile),
rotatelogs.WithRotationTime(time.Duration(_rotationHour)*time.Hour),
)
writeStdout := os.Stdout
logger.SetOutput(io.MultiWriter(writerFile, writeStdout))
// logger.SetReportCaller(true)
logger.SetFormatter(new(customFormatter))
// 設定log level
// logger.SetLevel(logrus.DebugLevel)
// 分級分檔案
// infoWriter, err := rotatelogs.New(
// _infoPath+".%Y-%m-%d",
// rotatelogs.WithLinkName(_infoPath),
// rotatelogs.WithRotationTime(time.Duration(_rotationHour)*time.Hour),
// rotatelogs.WithRotationCount(_countFile),
// )
// if err != nil {
// panic(err)
// }
// errorWriter, err := rotatelogs.New(
// _errorPath+".%Y-%m-%d",
// rotatelogs.WithLinkName(_errorPath),
// rotatelogs.WithRotationTime(time.Duration(_rotationHour)*time.Hour),
// rotatelogs.WithRotationCount(_countFile),
// )
// if err != nil {
// panic(err)
// }
// logger.AddHook(lfshook.NewHook(
// lfshook.WriterMap{
// logrus.InfoLevel: infoWriter,
// logrus.ErrorLevel: errorWriter,
// },
// &logrus.TextFormatter{
// DisableColors: true,
// TimestampFormat: _timeStampFormat,
// },
// ))
}
// Set level
func SetLevel(level int) {
// DEBUG: 5, INFO: 4, WARNING: 3, ERROR: 2, FATAL: 1
logger.SetLevel(logrus.Level(level))
}
// Debug : log.Debug
func Debug(input ...interface{}) {
_, file, line, _ := runtime.Caller(1)
file = filepath.Base(file)
msg := fmt.Sprintf("[%s:%d] %+v", file, line, input)
logger.Debug(msg)
}
// Info : log.info
func Info(input ...interface{}) {
_, file, line, _ := runtime.Caller(1)
file = filepath.Base(file)
msg := fmt.Sprintf("[%s:%d] %+v", file, line, input)
logger.Info(msg)
}
// Warn : log.Warn
func Warn(input ...interface{}) {
_, file, line, _ := runtime.Caller(1)
file = filepath.Base(file)
msg := fmt.Sprintf("[%s:%d] %+v", file, line, input)
logger.Warn(msg)
}
// Error : log.Error
func Error(input ...interface{}) {
_, file, line, _ := runtime.Caller(1)
file = filepath.Base(file)
msg := fmt.Sprintf("[%s:%d] %+v", file, line, input)
logger.Error(msg)
}
// Fatal : log.Fatal
func Fatal(input ...interface{}) {
_, file, line, _ := runtime.Caller(1)
file = filepath.Base(file)
// msg := fmt.Sprintf("[%s:%d] %1s", file, line, input)
msg := fmt.Sprintf("[%s:%d] %+v", file, line, input)
logger.Fatal(msg)
}
// Panic : log.Panic
func Panic(input ...interface{}) {
_, file, line, _ := runtime.Caller(1)
file = filepath.Base(file)
msg := fmt.Sprintf("[%s:%d] %+v", file, line, input)
logger.Panic(msg)
}
// func main() {
// // for i := 0; i < 15; i++ {
// for i := 0; ; i++ {
// if i%10 == 0 {
// log.Error("ERRORRRRRR!")
// } else {
// log.Info("hello, world!")
// }
// log.Debug("DDDDDDDDDDD")
// log.Warn("WWWWWWWWWWWW")
// time.Sleep(time.Duration(1) * time.Second)
// }
// }