5
5
"time"
6
6
7
7
"github.com/KyberNetwork/kutils/klog"
8
+ "github.com/KyberNetwork/logger"
8
9
"google.golang.org/grpc/codes"
9
10
"google.golang.org/grpc/metadata"
10
11
"google.golang.org/grpc/peer"
@@ -21,15 +22,30 @@ type InterceptorLogger interface {
21
22
// LoggerFunc is a function that also implements InterceptorLogger interface.
22
23
type LoggerFunc func (ctx context.Context , meta CallMeta , req any , resp any , err error , duration time.Duration )
23
24
25
+ // Log implements InterceptorLogger interface by triggering itself.
24
26
func (f LoggerFunc ) Log (ctx context.Context , meta CallMeta , req any , resp any , err error , duration time.Duration ) {
25
27
f (ctx , meta , req , resp , err , duration )
26
28
}
27
29
30
+ // opt is the option struct for logging interceptors.
28
31
type opt struct {
29
32
ignoreReq map [string ]struct {}
30
33
ignoreResp map [string ]struct {}
31
34
}
32
35
36
+ // newOpt returns a new opt struct with the given option funcs.
37
+ func newOpt (opts ... func (opt * opt )) * opt {
38
+ opt := & opt {
39
+ ignoreReq : make (map [string ]struct {}),
40
+ ignoreResp : make (map [string ]struct {}),
41
+ }
42
+ for _ , o := range opts {
43
+ o (opt )
44
+ }
45
+ return opt
46
+ }
47
+
48
+ // IgnoreReq ignores logging requests for commands with given specified full method names.
33
49
func IgnoreReq (ignoreReq ... string ) func (opt * opt ) {
34
50
return func (opt * opt ) {
35
51
for _ , v := range ignoreReq {
@@ -38,6 +54,7 @@ func IgnoreReq(ignoreReq ...string) func(opt *opt) {
38
54
}
39
55
}
40
56
57
+ // IgnoreResp ignores logging responses for commands with given specified full method names.
41
58
func IgnoreResp (ignoreResp ... string ) func (opt * opt ) {
42
59
return func (opt * opt ) {
43
60
for _ , v := range ignoreResp {
@@ -46,16 +63,17 @@ func IgnoreResp(ignoreResp ...string) func(opt *opt) {
46
63
}
47
64
}
48
65
66
+ // ignored is a string that indicates that the request or response is ignored.
49
67
const ignored = "<...>"
50
68
69
+ // DefaultLogger is the default logging interceptor which logs requests and responses in plain format.
51
70
func DefaultLogger (loggerFromCtx func (context.Context ) Logger , opts ... func (opt * opt )) LoggerFunc {
52
- opt := & opt {
53
- ignoreReq : make (map [string ]struct {}),
54
- ignoreResp : make (map [string ]struct {}),
55
- }
56
- for _ , o := range opts {
57
- o (opt )
71
+ if loggerFromCtx == nil {
72
+ loggerFromCtx = func (ctx context.Context ) Logger {
73
+ return klog .LoggerFromCtx (ctx )
74
+ }
58
75
}
76
+ opt := newOpt (opts ... )
59
77
return func (ctx context.Context , meta CallMeta , req any , resp any , err error , duration time.Duration ) {
60
78
code := status .Code (err )
61
79
if _ , ok := opt .ignoreReq [meta .FullMethod ]; ok {
@@ -74,8 +92,38 @@ func DefaultLogger(loggerFromCtx func(context.Context) Logger, opts ...func(opt
74
92
}
75
93
}
76
94
77
- func KlogLogger (ctx context.Context ) Logger {
78
- return klog .LoggerFromCtx (ctx )
95
+ // FieldsLogger is the fields-enabled logging interceptor which logs requests and responses in JSON format.
96
+ func FieldsLogger (loggerFromCtx func (context.Context ) logger.Logger , opts ... func (opt * opt )) LoggerFunc {
97
+ if loggerFromCtx == nil {
98
+ loggerFromCtx = func (ctx context.Context ) logger.Logger {
99
+ return klog .LoggerFromCtx (ctx )
100
+ }
101
+ }
102
+ opt := newOpt (opts ... )
103
+ return func (ctx context.Context , meta CallMeta , req any , resp any , err error , duration time.Duration ) {
104
+ code := status .Code (err )
105
+ if _ , ok := opt .ignoreReq [meta .FullMethod ]; ok {
106
+ req = ignored
107
+ }
108
+ if _ , ok := opt .ignoreResp [meta .FullMethod ]; ok {
109
+ resp = ignored
110
+ }
111
+ from := metadata .ValueFromIncomingContext (ctx , common .HeaderXForwardedFor )
112
+ if peerInfo , ok := peer .FromContext (ctx ); ok {
113
+ from = append (from , peerInfo .Addr .String ())
114
+ }
115
+ logFields := logger.Fields {
116
+ "cmd" : meta .FullMethod ,
117
+ "code" : code ,
118
+ "err" : err ,
119
+ "req" : req ,
120
+ "resp" : resp ,
121
+ "dur" : duration ,
122
+ "from" : from ,
123
+ }
124
+ log := loggerFromCtx (ctx ).WithFields (logFields )
125
+ Logf (log , code , "response sent" )
126
+ }
79
127
}
80
128
81
129
// Logger is a logger with infof/warnf/errorf methods.
0 commit comments