-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.go
45 lines (40 loc) · 1.09 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
package logit
import (
"runtime"
"strings"
"time"
"go.uber.org/zap/zapcore"
)
//
// FuncName
// @Description: FuncName 返回调用本函数的函数名称
// @param pc runtime.Caller 返回的第一个值
// @return string
//
func FuncName(pc uintptr) string {
funcName := runtime.FuncForPC(pc).Name()
sFuncName := strings.Split(funcName, ".")
return sFuncName[len(sFuncName)-1]
}
//
// CallerEncoder
// @Description: serializes a caller in package/file:funcname:line format
// @param caller
// @param enc
//
func CallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
shortCaller := caller.TrimmedPath()
shortCallerSplited := strings.Split(shortCaller, ":")
funcName := FuncName(caller.PC)
result := shortCallerSplited[0] + ":" + funcName + ":" + shortCallerSplited[1]
enc.AppendString(result)
}
//
// TimeEncoder
// @Description: 自定义日志时间格式, 不带时区信息, YYYY-mm-dd H:M:S.xxxxxx
// @param t
// @param enc
//
func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05.000000"))
}