-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_format.go
54 lines (49 loc) · 1.29 KB
/
log_format.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
package main
import (
"fmt"
"github.com/Iceinu-Project/IceGradient"
"github.com/sirupsen/logrus"
)
// LogFormatter 可以通过修改这个结构体的Format方法来设置你想要的日志格式
type LogFormatter struct{}
func (f *LogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
// 根据日志级别设置不同的颜色
var textColor string
var levelColor string
var levelText string
switch entry.Level {
case logrus.DebugLevel, logrus.TraceLevel:
levelColor = gradient.LightGreen
textColor = gradient.DarkGreen
levelText = "🚧DEBUG"
case logrus.InfoLevel:
levelColor = gradient.DarkCyan
textColor = gradient.Reset
levelText = "🧊INFO"
case logrus.WarnLevel:
levelColor = gradient.Orange
textColor = gradient.DarkOrange
levelText = "⚠️WARN"
case logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel:
levelColor = gradient.Red
textColor = gradient.Red
levelText = "🔴ERROR"
default:
levelColor = gradient.White
textColor = gradient.White
levelText = "UNKNOWN"
}
// 构建日志格式,可以按需修改
logMsg := fmt.Sprintf(
"%s• %s %s[%s%s%s] %s%s\n",
gradient.Gray,
entry.Time.Format("2006-01-02 15:04:05"),
textColor,
levelColor,
levelText,
textColor,
entry.Message,
gradient.Reset,
)
return []byte(logMsg), nil
}