Skip to content

Commit 3236156

Browse files
authored
Merge pull request #578 from a-simeshin/add-loggin-to-client-log-interceptor
2 parents 3dd7f2c + c69049c commit 3236156

File tree

2 files changed

+89
-7
lines changed

2 files changed

+89
-7
lines changed

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@
2424
import io.grpc.Channel;
2525
import io.grpc.ClientCall;
2626
import io.grpc.ClientInterceptor;
27+
import io.grpc.ForwardingClientCall;
28+
import io.grpc.ForwardingClientCallListener;
29+
import io.grpc.Metadata;
2730
import io.grpc.MethodDescriptor;
31+
import io.grpc.Status;
2832
import net.devh.boot.grpc.client.interceptor.GrpcGlobalClientInterceptor;
2933

3034
/**
31-
* Example {@link ClientInterceptor} that logs all called methods. In this example it is added to Spring's application
35+
* Example {@link ClientInterceptor} that logs all called methods in INFO log level, also request and response messages,
36+
* headers, trailers and interaction status in DEBUG log level. In this example it is added to Spring's application
3237
* context via {@link GlobalInterceptorConfiguration}, but is also possible to directly annotate this class with
3338
* {@link GrpcGlobalClientInterceptor}.
3439
*/
@@ -43,8 +48,40 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
4348
final CallOptions callOptions,
4449
final Channel next) {
4550

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

5087
}

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

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,19 @@
2424
import io.grpc.Channel;
2525
import io.grpc.ClientCall;
2626
import io.grpc.ClientInterceptor;
27+
import io.grpc.ForwardingClientCall;
28+
import io.grpc.ForwardingClientCallListener;
29+
import io.grpc.Metadata;
2730
import io.grpc.MethodDescriptor;
31+
import io.grpc.Status;
32+
import net.devh.boot.grpc.client.interceptor.GrpcGlobalClientInterceptor;
2833

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

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

4489
}

0 commit comments

Comments
 (0)