/
formatter.go
93 lines (86 loc) · 2.51 KB
/
formatter.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
package logger
import (
"fmt"
"github.com/hetianyi/easygo/timex"
. "github.com/logrusorgru/aurora"
"runtime"
"strconv"
"strings"
"time"
)
// DefaultFormatter 是默认的日志格式器,输出格式为
//
// [I] [PREFIX] 2022-03-02 21:35:43 | xxxxx
// [E] [PREFIX] 2022-03-02 21:35:43 | xxxxx
func DefaultFormatter(l *logger, t time.Time, level LogLevel, v ...interface{}) {
var log string
if l.ColorableOutput {
log = fmt.Sprintf("[%s] %s%s %s | %s\n",
levelColor(level), l.Prefix,
timeColor(timex.GetLongDateString(t)),
GetCaller(l.callLevel, l.ColorableOutput),
logColor(level, fmt.Sprint(v...)))
} else {
log = fmt.Sprintf("[%s] %s%s %s | %s\n",
levelShortNameMapping[level], l.Prefix,
timex.GetLongDateString(t),
GetCaller(l.callLevel, l.ColorableOutput),
fmt.Sprint(v...))
}
l.Out.Write([]byte(log))
}
// SimpleFormatter 是简化的日志格式器,输出格式为
//
// [I] [PREFIX] 2022-03-02 21:35:43 | xxxxx
// [E] [PREFIX] 2022-03-02 21:35:43 | xxxxx
func SimpleFormatter(l *logger, t time.Time, level LogLevel, v ...interface{}) {
log := fmt.Sprintf("[%s] %s%s | %s\n",
levelColor(level), l.Prefix,
timeColor(timex.GetLongDateString(t)),
logColor(level, fmt.Sprint(v...)))
l.Out.Write([]byte(log))
}
func levelColor(level LogLevel) string {
switch level {
case LevelDebug:
return BrightBlack(levelShortNameMapping[level]).String()
case LevelInfo:
return BrightGreen(levelShortNameMapping[level]).String()
case LevelWarn:
return BrightYellow(levelShortNameMapping[level]).String()
case LevelError:
return Red(levelShortNameMapping[level]).String()
case LevelFatal:
return SlowBlink(BgRed(levelShortNameMapping[level])).String()
}
return ""
}
func timeColor(t string) string {
return Cyan(t).String()
}
func logColor(level LogLevel, content string) string {
switch level {
case LevelDebug:
return Reverse(BrightBlack(content)).String()
case LevelInfo:
return content
case LevelWarn:
return BrightYellow(content).String()
case LevelError:
return Red(content).String()
case LevelFatal:
return SlowBlink(BgRed(content)).String()
}
return content
}
func GetCaller(callLevel int, colorable bool) string {
_, file, line, success := runtime.Caller(callLevel)
if success {
if colorable {
return Yellow(strings.Join([]string{"[", file[strings.LastIndex(file, "/")+1:], ":", strconv.Itoa(line), "]"}, "")).String()
} else {
return strings.Join([]string{"[", file[strings.LastIndex(file, "/")+1:], ":", strconv.Itoa(line), "]"}, "")
}
}
return " [unknown] "
}