OkHttp 异步调用替换日志中的线程名称

问题描述 投票:0回答:0

我使用 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: ...
java java-8 okhttp
© www.soinside.com 2019 - 2024. All rights reserved.