为何Logbook未记录外部请求?附测试代码与日志
Logbook无法记录外部请求问题排查求助
我遇到Logbook无法记录外部请求的问题,以下是单元测试代码及运行产生的日志,恳请协助排查原因:
单元测试代码
@Test public void logRequestBodyTest() { Logbook logbook = Logbook.builder() .sink(new DefaultSink( new DefaultHttpLogFormatter(), new DefaultHttpLogWriter() )) .build(); final LogbookClientFilter filter = new LogbookClientFilter(logbook); final LogbookHttpRequestInterceptor interceptor = new LogbookHttpRequestInterceptor(logbook); final ResteasyClient client = new ResteasyClientBuilder().register(filter).build(); final Response response = client.target("http://localhost:15555/test/management").request().get(); }
运行日志
[15:51:13,553] [DEBUG] [main][,,][RequestAddCookies.process(123)] CookieSpec selected: default [15:51:13,560] [DEBUG] [main][,,][RequestAuthCache.process(77)] Auth cache not set in the context [15:51:13,560] [DEBUG] [main][,,][PoolingHttpClientConnectionManager.requestConnection(265)] Connection request: [route: {}->http://localhost:15555][total kept alive: 0; route allocated: 0 of 50; total allocated: 0 of 50] [15:51:13,577] [DEBUG] [main][,,][PoolingHttpClientConnectionManager.leaseConnection(309)] Connection leased: [id: 0][route: {}->http://localhost:15555][total kept alive: 0; route allocated: 1 of 50; total allocated: 1 of 50] [15:51:13,577] [DEBUG] [main][,,][MainClientExec.execute(235)] Opening connection {}->http://localhost:15555 [15:51:13,596] [DEBUG] [main][,,][DefaultHttpClientConnectionOperator.connect(139)] Connecting to localhost/127.0.0.1:15555 [15:51:13,597] [DEBUG] [main][,,][DefaultHttpClientConnectionOperator.connect(146)] Connection established 127.0.0.1:53944<->127.0.0.1:15555 [15:51:13,597] [DEBUG] [main][,,][MainClientExec.execute(256)] Executing request GET /test/management HTTP/1.1 [15:51:13,597] [DEBUG] [main][,,][MainClientExec.execute(261)] Target auth state: UNCHALLENGED [15:51:13,597] [DEBUG] [main][,,][MainClientExec.execute(267)] Proxy auth state: UNCHALLENGED [15:51:13,597] [DEBUG] [main][,,][headers.onRequestSubmitted(133)] http-outgoing-0 >> GET /test/management HTTP/1.1 [15:51:13,597] [DEBUG] [main][,,][headers.onRequestSubmitted(136)] http-outgoing-0 >> Host: localhost:15555 [15:51:13,597] [DEBUG] [main][,,][headers.onRequestSubmitted(136)] http-outgoing-0 >> Connection: Keep-Alive [15:51:13,597] [DEBUG] [main][,,][headers.onRequestSubmitted(136)] http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.4 (Java/11.0.2) [15:51:13,630] [DEBUG] [main][,,][headers.onResponseReceived(122)] http-outgoing-0 << HTTP/1.0 400 Bad Request [15:51:13,664] [DEBUG] [Finalizer][,,][DefaultManagedHttpClientConnection.close(79)] http-outgoing-0: Close connection
排查与解决建议
日志级别未配置:当前日志仅输出Apache HttpClient的DEBUG信息,没有Logbook的日志。需在日志框架(如Logback/Log4j)中添加Logbook的DEBUG级别配置,比如:
<!-- Logback配置示例 --> <logger name="org.zalando.logbook" level="DEBUG"/>过滤器注册问题:你创建了
LogbookHttpRequestInterceptor但未使用,且LogbookClientFilter作为JAX-RS过滤器,需确保被Resteasy正确加载。可尝试指定过滤器优先级,确保其优先执行:final ResteasyClient client = new ResteasyClientBuilder() .register(filter, Priorities.HEADER_DECORATOR) .build();切换到Apache HttpClient拦截器方案:如果JAX-RS过滤器方案不生效,可直接操作Resteasy底层的HttpClient,注册Logbook的Apache拦截器:
HttpClient httpClient = HttpClientBuilder.create() .addInterceptorFirst(new LogbookHttpRequestInterceptor(logbook)) .addInterceptorFirst(new LogbookHttpResponseInterceptor(logbook)) .build(); final ResteasyClient client = new ResteasyClientBuilder() .httpEngine(new ApacheHttpClient4Engine(httpClient)) .build();
内容的提问来源于stack exchange,提问作者damianoo
相关产品推荐
相关产品推荐

