-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
66 lines (59 loc) · 1.33 KB
/
encoder.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
package zero_mdc_log
import (
"strconv"
"strings"
"github.com/rs/zerolog"
)
// 这个为什么叫Lowercase呢,默认就是小写?
func LowercaseLevelEncoder() func(l zerolog.Level) string {
return func(l zerolog.Level) string {
return l.String()
}
}
func UppercaseLevelEncoder() func(l zerolog.Level) string {
return func(l zerolog.Level) string {
switch l {
case zerolog.TraceLevel:
return "TRACE"
case zerolog.DebugLevel:
return "DEBUG"
case zerolog.InfoLevel:
return "INFO"
case zerolog.WarnLevel:
return "WARN"
case zerolog.ErrorLevel:
return "ERROR"
case zerolog.FatalLevel:
return "FATAL"
case zerolog.PanicLevel:
return "PANIC"
case zerolog.NoLevel:
return ""
}
return ""
}
}
// 拼接文件和行号
func FullCallerEncoder() func(file string, line int) string {
return func(file string, line int) string {
return file + ":" + strconv.Itoa(line)
}
}
// 只有文件没有路径
func ShortCallerEncoder() func(file string, line int) string {
return func(file string, line int) string {
return TrimmedPath(file) + ":" + strconv.Itoa(line)
}
}
// 只有文件没有路径
func TrimmedPath(file string) string {
idx := strings.LastIndexByte(file, '/')
if idx == -1 {
return file
}
idx = strings.LastIndexByte(file[:idx], '/')
if idx == -1 {
return file
}
return file[idx+1:]
}