2727 iserr func (codes.Code ) bool
2828 disableCallLogging bool
2929 disableCallID bool
30+ logFunc func (ctx context.Context , keyvals ... Fielder )
3031 }
3132)
3233
@@ -43,6 +44,12 @@ func UnaryServerInterceptor(logCtx context.Context, opts ...GRPCLogOption) grpc.
4344 for _ , opt := range opts {
4445 opt (o )
4546 }
47+
48+ logFunc := Print
49+ if o .logFunc != nil {
50+ logFunc = o .logFunc
51+ }
52+
4653 return func (
4754 ctx context.Context ,
4855 req interface {},
@@ -59,7 +66,7 @@ func UnaryServerInterceptor(logCtx context.Context, opts ...GRPCLogOption) grpc.
5966 then := time .Now ()
6067 svcKV := KV {K : GRPCServiceKey , V : path .Dir (info .FullMethod )[1 :]}
6168 methKV := KV {K : GRPCMethodKey , V : path .Base (info .FullMethod )}
62- Print (ctx , KV {MessageKey , "start" }, svcKV , methKV )
69+ logFunc (ctx , KV {MessageKey , "start" }, svcKV , methKV )
6370
6471 res , err := handler (ctx , req )
6572
@@ -72,7 +79,7 @@ func UnaryServerInterceptor(logCtx context.Context, opts ...GRPCLogOption) grpc.
7279 Error (ctx , err , svcKV , methKV , statKV , codeKV , durKV )
7380 return res , err
7481 }
75- Print (ctx , KV {MessageKey , "end" }, svcKV , methKV , codeKV , durKV )
82+ logFunc (ctx , KV {MessageKey , "end" }, svcKV , methKV , codeKV , durKV )
7683 return res , err
7784 }
7885}
@@ -87,6 +94,12 @@ func StreamServerInterceptor(logCtx context.Context, opts ...GRPCLogOption) grpc
8794 for _ , opt := range opts {
8895 opt (o )
8996 }
97+
98+ logFunc := Print
99+ if o .logFunc != nil {
100+ logFunc = o .logFunc
101+ }
102+
90103 return func (
91104 srv interface {},
92105 stream grpc.ServerStream ,
@@ -104,7 +117,7 @@ func StreamServerInterceptor(logCtx context.Context, opts ...GRPCLogOption) grpc
104117 then := time .Now ()
105118 svcKV := KV {K : GRPCServiceKey , V : path .Dir (info .FullMethod )[1 :]}
106119 methKV := KV {K : GRPCMethodKey , V : path .Base (info .FullMethod )}
107- Print (ctx , KV {MessageKey , "start" }, svcKV , methKV )
120+ logFunc (ctx , KV {MessageKey , "start" }, svcKV , methKV )
108121
109122 err := handler (srv , stream )
110123
@@ -117,7 +130,7 @@ func StreamServerInterceptor(logCtx context.Context, opts ...GRPCLogOption) grpc
117130 Error (ctx , err , svcKV , methKV , statKV , codeKV , durKV )
118131 return err
119132 }
120- Print (ctx , KV {MessageKey , "end" }, svcKV , methKV , codeKV , durKV )
133+ logFunc (ctx , KV {MessageKey , "end" }, svcKV , methKV , codeKV , durKV )
121134 return err
122135 }
123136}
@@ -129,6 +142,12 @@ func UnaryClientInterceptor(opts ...GRPCLogOption) grpc.UnaryClientInterceptor {
129142 for _ , opt := range opts {
130143 opt (o )
131144 }
145+
146+ logFunc := Print
147+ if o .logFunc != nil {
148+ logFunc = o .logFunc
149+ }
150+
132151 return func (
133152 ctx context.Context ,
134153 fullmethod string ,
@@ -140,7 +159,7 @@ func UnaryClientInterceptor(opts ...GRPCLogOption) grpc.UnaryClientInterceptor {
140159 then := time .Now ()
141160 svcKV := KV {K : GRPCServiceKey , V : path .Dir (fullmethod )[1 :]}
142161 methKV := KV {K : GRPCMethodKey , V : path .Base (fullmethod )}
143- Print (ctx , KV {K : MessageKey , V : "start" }, svcKV , methKV )
162+ logFunc (ctx , KV {K : MessageKey , V : "start" }, svcKV , methKV )
144163
145164 err := invoker (ctx , fullmethod , req , reply , cc , opts ... )
146165
@@ -153,7 +172,7 @@ func UnaryClientInterceptor(opts ...GRPCLogOption) grpc.UnaryClientInterceptor {
153172 Error (ctx , err , svcKV , methKV , statKV , codeKV , durKV )
154173 return err
155174 }
156- Print (ctx , KV {K : MessageKey , V : "end" }, svcKV , methKV , codeKV , durKV )
175+ logFunc (ctx , KV {K : MessageKey , V : "end" }, svcKV , methKV , codeKV , durKV )
157176 return err
158177 }
159178}
@@ -165,6 +184,12 @@ func StreamClientInterceptor(opts ...GRPCLogOption) grpc.StreamClientInterceptor
165184 for _ , opt := range opts {
166185 opt (o )
167186 }
187+
188+ logFunc := Print
189+ if o .logFunc != nil {
190+ logFunc = o .logFunc
191+ }
192+
168193 return func (
169194 ctx context.Context ,
170195 desc * grpc.StreamDesc ,
@@ -176,7 +201,7 @@ func StreamClientInterceptor(opts ...GRPCLogOption) grpc.StreamClientInterceptor
176201 then := time .Now ()
177202 svcKV := KV {K : GRPCServiceKey , V : path .Dir (fullmethod )[1 :]}
178203 methKV := KV {K : GRPCMethodKey , V : path .Base (fullmethod )}
179- Print (ctx , KV {K : MessageKey , V : "start" }, svcKV , methKV )
204+ logFunc (ctx , KV {K : MessageKey , V : "start" }, svcKV , methKV )
180205
181206 stream , err := streamer (ctx , desc , cc , fullmethod , opts ... )
182207
@@ -189,7 +214,7 @@ func StreamClientInterceptor(opts ...GRPCLogOption) grpc.StreamClientInterceptor
189214 Error (ctx , err , svcKV , methKV , statKV , codeKV , durKV )
190215 return stream , err
191216 }
192- Print (ctx , KV {K : MessageKey , V : "end" }, svcKV , methKV , codeKV , durKV )
217+ logFunc (ctx , KV {K : MessageKey , V : "end" }, svcKV , methKV , codeKV , durKV )
193218 return stream , err
194219 }
195220}
@@ -218,6 +243,14 @@ func WithDisableCallID() GRPCLogOption {
218243 }
219244}
220245
246+ // WithCallLogFunc returns a HTTP middleware option that configures the logger to use
247+ // the given log function instead of log.Print() as default.
248+ func WithCallLogFunc (logFunc func (ctx context.Context , keyvals ... Fielder )) GRPCLogOption {
249+ return func (o * grpcOptions ) {
250+ o .logFunc = logFunc
251+ }
252+ }
253+
221254func defaultGRPCOptions () * grpcOptions {
222255 return & grpcOptions {
223256 iserr : func (c codes.Code ) bool {
0 commit comments