@@ -42,6 +42,7 @@ public class TracingMessageConsumer implements MessageConsumer {
42
42
private final PrintWriter printWriter ;
43
43
private final Clock clock ;
44
44
private final DateTimeFormatter dateTimeFormatter ;
45
+ private MessageJsonHandler jsonHandler ;
45
46
46
47
/**
47
48
* @param messageConsumer The {@link MessageConsumer} to wrap.
@@ -86,6 +87,10 @@ public TracingMessageConsumer(
86
87
}
87
88
}
88
89
90
+ public void setJsonHandler (MessageJsonHandler jsonHandler ) {
91
+ this .jsonHandler = jsonHandler ;
92
+ }
93
+
89
94
/**
90
95
* Constructs a log string for a given {@link Message}. The type of the {@link MessageConsumer}
91
96
* determines if we're sending or receiving a message. The type of the @{link Message} determines
@@ -122,33 +127,33 @@ private String consumeMessageSending(Message message, Instant now, String date)
122
127
RequestMetadata requestMetadata = new RequestMetadata (method , now );
123
128
sentRequests .put (id , requestMetadata );
124
129
Object params = requestMessage .getParams ();
125
- String paramsJson = MessageJsonHandler . toString (params );
130
+ String paramsJson = toString (params );
126
131
String format = "[Trace - %s] Sending request '%s - (%s)'\n Params: %s\n \n \n " ;
127
132
return String .format (format , date , method , id , paramsJson );
128
133
} else if (message instanceof ResponseMessage ) {
129
134
ResponseMessage responseMessage = (ResponseMessage ) message ;
130
135
String id = responseMessage .getId ();
131
136
RequestMetadata requestMetadata = receivedRequests .remove (id );
132
137
if (requestMetadata == null ) {
133
- LOG .log (WARNING , String .format ("Unmatched response message: %s" , message ));
138
+ LOG .log (WARNING , String .format ("Unmatched response message: %s" , toString ( message ) ));
134
139
return null ;
135
140
}
136
141
String method = requestMetadata .method ;
137
142
long latencyMillis = now .toEpochMilli () - requestMetadata .start .toEpochMilli ();
138
143
Object result = responseMessage .getResult ();
139
- String resultJson = MessageJsonHandler . toString (result );
144
+ String resultJson = toString (result );
140
145
String format =
141
146
"[Trace - %s] Sending response '%s - (%s)'. Processing request took %sms\n Result: %s\n \n \n " ;
142
147
return String .format (format , date , method , id , latencyMillis , resultJson );
143
148
} else if (message instanceof NotificationMessage ) {
144
149
NotificationMessage notificationMessage = (NotificationMessage ) message ;
145
150
String method = notificationMessage .getMethod ();
146
151
Object params = notificationMessage .getParams ();
147
- String paramsJson = MessageJsonHandler . toString (params );
152
+ String paramsJson = toString (params );
148
153
String format = "[Trace - %s] Sending notification '%s'\n Params: %s\n \n \n " ;
149
154
return String .format (format , date , method , paramsJson );
150
155
} else {
151
- LOG .log (WARNING , String .format ("Unknown message type: %s" , message ));
156
+ LOG .log (WARNING , String .format ("Unknown message type: %s" , toString ( message ) ));
152
157
return null ;
153
158
}
154
159
}
@@ -161,38 +166,42 @@ private String consumeMessageReceiving(Message message, Instant now, String date
161
166
RequestMetadata requestMetadata = new RequestMetadata (method , now );
162
167
receivedRequests .put (id , requestMetadata );
163
168
Object params = requestMessage .getParams ();
164
- String paramsJson = MessageJsonHandler . toString (params );
169
+ String paramsJson = toString (params );
165
170
String format = "[Trace - %s] Received request '%s - (%s)'\n Params: %s\n \n \n " ;
166
171
return String .format (format , date , method , id , paramsJson );
167
172
} else if (message instanceof ResponseMessage ) {
168
173
ResponseMessage responseMessage = (ResponseMessage ) message ;
169
174
String id = responseMessage .getId ();
170
175
RequestMetadata requestMetadata = sentRequests .remove (id );
171
176
if (requestMetadata == null ) {
172
- LOG .log (WARNING , String .format ("Unmatched response message: %s" , message ));
177
+ LOG .log (WARNING , String .format ("Unmatched response message: %s" , toString ( message ) ));
173
178
return null ;
174
179
}
175
180
String method = requestMetadata .method ;
176
181
long latencyMillis = now .toEpochMilli () - requestMetadata .start .toEpochMilli ();
177
182
Object result = responseMessage .getResult ();
178
- String resultJson = MessageJsonHandler . toString (result );
183
+ String resultJson = toString (result );
179
184
Object error = responseMessage .getError ();
180
- String errorJson = MessageJsonHandler . toString (error );
185
+ String errorJson = toString (error );
181
186
String format = "[Trace - %s] Received response '%s - (%s)' in %sms\n Result: %s\n Error: %s\n \n \n " ;
182
187
return String .format (format , date , method , id , latencyMillis , resultJson , errorJson );
183
188
} else if (message instanceof NotificationMessage ) {
184
189
NotificationMessage notificationMessage = (NotificationMessage ) message ;
185
190
String method = notificationMessage .getMethod ();
186
191
Object params = notificationMessage .getParams ();
187
- String paramsJson = MessageJsonHandler . toString (params );
192
+ String paramsJson = toString (params );
188
193
String format = "[Trace - %s] Received notification '%s'\n Params: %s\n \n \n " ;
189
194
return String .format (format , date , method , paramsJson );
190
195
} else {
191
- LOG .log (WARNING , String .format ("Unknown message type: %s" , message ));
196
+ LOG .log (WARNING , String .format ("Unknown message type: %s" , toString ( message ) ));
192
197
return null ;
193
198
}
194
199
}
195
200
201
+ private String toString (Object object ) {
202
+ return jsonHandler != null ? jsonHandler .format (object ) : MessageJsonHandler .toString (object );
203
+ }
204
+
196
205
/** Data class for holding pending request metadata. */
197
206
public static class RequestMetadata {
198
207
final String method ;
0 commit comments