-
Notifications
You must be signed in to change notification settings - Fork 33
/
print_run_log.go
108 lines (100 loc) · 3.11 KB
/
print_run_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
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 drpc
import (
"context"
"encoding/json"
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/util/gconv"
"github.com/osgochina/dmicro/drpc/internal"
"github.com/osgochina/dmicro/drpc/message"
"strconv"
"time"
)
const (
typePushLaunch int8 = 1
typePushHandle int8 = 2
typeCallLaunch int8 = 3
typeCallHandle int8 = 4
)
const (
logFormatPushLaunch = "PUSH-> %s %s %q SEND(%s)"
logFormatPushHandle = "PUSH<- %s %s %q RECV(%s)"
logFormatCallLaunch = "CALL-> %s %s %q SEND(%s) RECV(%s)"
logFormatCallHandle = "CALL<- %s %s %q RECV(%s) SEND(%s)"
)
func enablePrintRunLog() bool {
return internal.GetLevel()&glog.LEVEL_DEBU > 0
}
//打印运行log
func (that *session) printRunLog(realIP string, costTime time.Duration, input, output message.Message, logType int8) {
defer func() {
if err := recover(); err != nil {
internal.Error(context.TODO(), err)
}
}()
var addr = that.RemoteAddr().String()
if realIP != "" && realIP == addr {
realIP = "same"
}
if realIP == "" {
realIP = "-"
}
addr += "(real:" + realIP + ")"
var (
costTimeStr string
printFunc = internal.Debugf
)
// 如果定义了慢处理时间
if that.endpoint.slowCometDuration > 0 {
if costTime >= that.endpoint.slowCometDuration {
costTimeStr = costTime.String() + "(slow)"
} else {
costTimeStr = costTime.String() + "(fast)"
}
} else {
costTimeStr = "(-)"
}
switch logType {
case typePushLaunch:
printFunc(context.TODO(), logFormatPushLaunch, addr, costTimeStr, output.ServiceMethod(), messageLogBytes(output, that.endpoint.printDetail))
case typePushHandle:
printFunc(context.TODO(), logFormatPushHandle, addr, costTimeStr, input.ServiceMethod(), messageLogBytes(input, that.endpoint.printDetail))
case typeCallLaunch:
printFunc(context.TODO(), logFormatCallLaunch, addr, costTimeStr, output.ServiceMethod(), messageLogBytes(output, that.endpoint.printDetail), messageLogBytes(input, that.endpoint.printDetail))
case typeCallHandle:
printFunc(context.TODO(), logFormatCallHandle, addr, costTimeStr, input.ServiceMethod(), messageLogBytes(input, that.endpoint.printDetail), messageLogBytes(output, that.endpoint.printDetail))
}
}
func messageLogBytes(message message.Message, printDetail bool) []byte {
var b = make([]byte, 0, 128)
b = append(b, '{')
b = append(b, '"', 's', 'i', 'z', 'e', '"', ':')
b = append(b, strconv.FormatUint(uint64(message.Size()), 10)...)
if statBytes := message.Status().String(); len(statBytes) > 0 {
b = append(b, ',', '"', 's', 't', 'a', 't', 'u', 's', '"', ':')
b = append(b, statBytes...)
}
if printDetail {
if message.Meta().Size() > 0 {
b = append(b, ',', '"', 'm', 'e', 't', 'a', '"', ':')
b = append(b, gconv.Bytes(message.Meta().String())...)
}
if bodyBytes := bodyLogBytes(message); len(bodyBytes) > 0 {
b = append(b, ',', '"', 'b', 'o', 'd', 'y', '"', ':')
b = append(b, bodyBytes...)
}
}
b = append(b, '}')
return b
}
func bodyLogBytes(message message.Message) []byte {
switch v := message.Body().(type) {
case nil:
return nil
case []byte:
return gconv.Bytes(v)
case *[]byte:
return gconv.Bytes(v)
}
b, _ := json.Marshal(message.Body())
return b
}