我使用 okhttp 客户端进行异步调用并记录请求和响应...
public void sendRequest(String requestJson) {
LOG.info("Sending payload: {}", requestJson);
Request httpRequest = buildRequest(requestJson, url);
okHttpClient.newCall(httpRequest).enqueue(new Callback() {
@Override
public void onResponse(@Nonnull Call call, @Nonnull Response response) {
LOG.info("Received response: {}", response.body().string());
// Do things with response...
}
}
}
这很好用。但是,我的日志记录有问题。请求日志显示正常,其中 %t 替换为我需要的值,即本例中的设备 ID。但是,由于异步调用,该线程名称被替换为
OkHttp http://localhost:8080/...
。就像下面这样;
[11:54:29:025] INFO [Device_778445] TestDeviceClient - Sending payload:...
[11:54:30:011] INFO [OkHttp http://localhost:8080/...] TestDeviceClient - Received response: ...
是否可以在异步 okhttp 请求中保留 %t/线程名称值?
简而言之,我要找的是:
[11:54:29:025] INFO [Device_778445] TestDeviceClient - Sending payload:...
[11:54:30:011] INFO [Device_778445] TestDeviceClient - Received response: ...