Skip to content

Commit 641c5dc

Browse files
committed
Add to LogGrpcInterceptor example logging for all grpc data for DEBUG logging level
1 parent 3e4deca commit 641c5dc

File tree

2 files changed

+89
-11
lines changed

2 files changed

+89
-11
lines changed

examples/cloud-grpc-client/src/main/java/net/devh/boot/grpc/examples/cloud/client/LogGrpcInterceptor.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@
1717

1818
package net.devh.boot.grpc.examples.cloud.client;
1919

20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
22-
2320
import io.grpc.CallOptions;
2421
import io.grpc.Channel;
2522
import io.grpc.ClientCall;
2623
import io.grpc.ClientInterceptor;
24+
import io.grpc.ForwardingClientCall;
25+
import io.grpc.ForwardingClientCallListener;
26+
import io.grpc.Metadata;
2727
import io.grpc.MethodDescriptor;
28+
import io.grpc.Status;
29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
2831
import net.devh.boot.grpc.client.interceptor.GrpcGlobalClientInterceptor;
2932

3033
/**
31-
* Example {@link ClientInterceptor} that logs all called methods. In this example it is added to Spring's application
34+
* Example {@link ClientInterceptor} that logs all called methods in INFO log level, also request and response messages,
35+
* headers, trailers and interaction status in DEBUG log level. In this example it is added to Spring's application
3236
* context via {@link GlobalInterceptorConfiguration}, but is also possible to directly annotate this class with
3337
* {@link GrpcGlobalClientInterceptor}.
3438
*/
@@ -44,7 +48,38 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
4448
final Channel next) {
4549

4650
log.info(method.getFullMethodName());
47-
return next.newCall(method, callOptions);
51+
return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
52+
53+
@Override
54+
public void sendMessage(ReqT message) {
55+
log.debug("Request message: \n{}", message.toString());
56+
super.sendMessage(message);
57+
}
58+
59+
@Override
60+
public void start(Listener<RespT> responseListener, Metadata headers) {
61+
super.start(new ForwardingClientCallListener.SimpleForwardingClientCallListener<RespT>(responseListener) {
62+
@Override
63+
public void onMessage(RespT message) {
64+
log.debug("Response message: \n{}", message.toString());
65+
super.onMessage(message);
66+
}
67+
68+
@Override
69+
public void onHeaders(Metadata headers) {
70+
log.debug("gRPC headers: \n{}", headers.toString());
71+
super.onHeaders(headers);
72+
}
73+
74+
@Override
75+
public void onClose(Status status, Metadata trailers) {
76+
log.info("Interaction ends with status: {}", status.toString());
77+
log.info("Trailers: {}", trailers.toString());
78+
super.onClose(status, trailers);
79+
}
80+
}, headers);
81+
}
82+
};
4883
}
4984

5085
}

examples/local-grpc-client/src/main/java/net/devh/boot/grpc/examples/local/client/LogGrpcInterceptor.java

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,25 @@
1717

1818
package net.devh.boot.grpc.examples.local.client;
1919

20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
22-
2320
import io.grpc.CallOptions;
2421
import io.grpc.Channel;
2522
import io.grpc.ClientCall;
2623
import io.grpc.ClientInterceptor;
24+
import io.grpc.ForwardingClientCall;
25+
import io.grpc.ForwardingClientCallListener;
26+
import io.grpc.Metadata;
2727
import io.grpc.MethodDescriptor;
28+
import io.grpc.Status;
29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
31+
import net.devh.boot.grpc.client.interceptor.GrpcGlobalClientInterceptor;
2832

2933
/**
34+
* Example {@link ClientInterceptor} that logs all called methods in INFO log level, also request and response messages,
35+
* headers, trailers and interaction status in DEBUG log level. In this example it is added to Spring's application
36+
* context via {@link GlobalClientInterceptorConfiguration}, but is also possible to directly annotate this class with
37+
* {@link GrpcGlobalClientInterceptor}.
38+
*
3039
* @author Michael ([email protected])
3140
* @since 2016/12/8
3241
*/
@@ -35,10 +44,44 @@ public class LogGrpcInterceptor implements ClientInterceptor {
3544
private static final Logger log = LoggerFactory.getLogger(LogGrpcInterceptor.class);
3645

3746
@Override
38-
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
39-
CallOptions callOptions, Channel next) {
47+
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
48+
MethodDescriptor<ReqT, RespT> method,
49+
CallOptions callOptions,
50+
Channel next) {
51+
4052
log.info(method.getFullMethodName());
41-
return next.newCall(method, callOptions);
53+
return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
54+
55+
@Override
56+
public void sendMessage(ReqT message) {
57+
log.debug("Request message: \n{}", message.toString());
58+
super.sendMessage(message);
59+
}
60+
61+
@Override
62+
public void start(Listener<RespT> responseListener, Metadata headers) {
63+
super.start(new ForwardingClientCallListener.SimpleForwardingClientCallListener<RespT>(responseListener) {
64+
@Override
65+
public void onMessage(RespT message) {
66+
log.debug("Response message: \n{}", message.toString());
67+
super.onMessage(message);
68+
}
69+
70+
@Override
71+
public void onHeaders(Metadata headers) {
72+
log.debug("gRPC headers: \n{}", headers.toString());
73+
super.onHeaders(headers);
74+
}
75+
76+
@Override
77+
public void onClose(Status status, Metadata trailers) {
78+
log.info("Interaction ends with status: {}", status.toString());
79+
log.info("Trailers: {}", trailers.toString());
80+
super.onClose(status, trailers);
81+
}
82+
}, headers);
83+
}
84+
};
4285
}
4386

4487
}

0 commit comments

Comments
 (0)