-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatterhelper.go
108 lines (92 loc) · 2.53 KB
/
formatterhelper.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
package qlog
import (
"fmt"
"os"
"path/filepath"
"reflect"
"runtime"
"github.com/sirupsen/logrus"
)
const (
longTimeStamp = "2006/01/02 15:04:05.000000Z07:00"
shortTimeStamp = "06/01/02 15:04:05.000"
keyPrettyCaller = "prettycaller" // omitfunc, truncated
)
var (
gRegisteredFormatters = make(map[string]reflect.Type)
gPrettyCallFuncMap = map[string]func(*runtime.Frame) (function string, file string){
"omitfunc": prettyCallerOmitFunc,
"truncated": prettyCallerTruncated,
}
)
func registeFormatter(name string, typ reflect.Type) {
gRegisteredFormatters[name] = typ
}
// copy from filepath.Base
func truncatedPath(path string) string {
if path == "" {
return "."
}
// Strip trailing slashes.
for len(path) > 0 && os.IsPathSeparator(path[len(path)-1]) {
path = path[0 : len(path)-1]
}
// Throw away volume name
path = path[len(filepath.VolumeName(path)):]
// Find the last element
i := len(path) - 1
for i >= 0 && !os.IsPathSeparator(path[i]) {
i--
}
if i >= 0 {
// find first dir
j := i - 1
for j >= 0 && !os.IsPathSeparator(path[j]) {
j--
}
if j >= 0 {
path = path[j+1:]
}
}
// If empty now, it had only slashes.
if path == "" {
return string(filepath.Separator)
}
return path
}
func prettyCallerTruncated(caller *runtime.Frame) (function string, file string) {
//funcVal := ""
fileVal := fmt.Sprintf("%s:%d", truncatedPath(caller.File), caller.Line)
return "", fileVal
}
func prettyCallerOmitFunc(caller *runtime.Frame) (function string, file string) {
fileVal := fmt.Sprintf("%s:%d", caller.File, caller.Line)
return "", fileVal
}
func newFormatter(name string, key string) (logrus.Formatter, error) {
var err error
var typ reflect.Type
var ok bool
if typ, ok = gRegisteredFormatters[name]; !ok {
return nil, fmt.Errorf("[qlog] formatter name(%s) not registered", name)
}
f := reflect.New(typ)
if err = v.UnmarshalKey(key, f.Interface()); err != nil {
return nil, err
}
// check if we need truncate caller
prettyCaller := v.GetString(key + "." + keyPrettyCaller)
if len(prettyCaller) > 0 {
if prettyFunc, ok := gPrettyCallFuncMap[prettyCaller]; ok {
prettyFuncField := f.Elem().FieldByName("CallerPrettyfier")
if prettyFuncField.IsValid() {
prettyFuncField.Set(reflect.ValueOf(prettyFunc))
} else {
return nil, fmt.Errorf("[qlog] formatter name(%s) doesn't support truncate caller", name)
}
} else {
return nil, fmt.Errorf("[qlog] formatter name(%s) init with unsupported pretty func:%s", name, prettyCaller)
}
}
return f.Interface().(logrus.Formatter), nil
}