This repository has been archived by the owner on Jun 17, 2023. It is now read-only.
/
log.go
76 lines (64 loc) · 1.77 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
package gonebot
import (
"fmt"
"path"
"regexp"
"github.com/sirupsen/logrus"
)
// import "github.com/sirupsen/logrus"
type formatter struct{}
func (f *formatter) Format(entry *logrus.Entry) ([]byte, error) {
// red := "\033[31m%s\033[0m"
// green := "\033[32m%s\033[0m"
// yellow := "\033[33m%s\033[0m"
// blue := "\033[34m%s\033[0m"
// magenta := "\033[35m%s\033[0m"
// cyan := "\033[36m%s\033[0m"
// white := "\033[37m%s\033[0m"
result := ""
base := fmt.Sprintf("%s [%s]", entry.Time.Format("2006-01-02 15:04:05"), entry.Level.String())
if entry.Caller != nil {
fullName := entry.Caller.Function
pattern := regexp.MustCompile(`^(?:(.+)\/)?([^\/]+?)\.([^\/]+)$`)
matches := pattern.FindStringSubmatch(fullName)
// pkgPath := matches[1] // 上级路径
pkgName := matches[2]
funcName := matches[3]
_, filename := path.Split(entry.Caller.File)
if entry.Level <= logrus.ErrorLevel {
result = fmt.Sprintf(
"%s %s | %s | %s - line %d - %s",
base,
pkgName,
entry.Message,
filename,
entry.Caller.Line,
funcName,
)
} else {
result = fmt.Sprintf("%s %s | %s", base, pkgName, entry.Message)
}
} else {
result = fmt.Sprintf("%s %s", base, entry.Message)
}
fields := entry.Data
if len(fields) > 0 {
for k, v := range fields {
result += fmt.Sprintf(" | %s: %v", k, v)
}
}
return []byte(result + "\n"), nil
}
func withStyle(s string, color int, background int, style int) string {
return fmt.Sprintf("\033[%d;%d;%dm%s\033[0m", style, color, background, s)
}
func init() {
logrus.SetLevel(logrus.DebugLevel)
// logrus.SetFormatter(&logrus.TextFormatter{
// TimestampFormat: "2006-01-02 15:04:05",
// FullTimestamp: true,
// ForceColors: true,
// })
logrus.SetFormatter(&formatter{})
logrus.SetReportCaller(true)
}