Logging in JDK11 HttpClient
Asked Answered
E

2

5

JDK11 introduced a new HTTP Client, with many features that lacks in traditional java.net.HttpURLConnection class. First question that I encountered with is how to properly enable logging in newly added HTTP Client?

Erupt answered 17/2, 2019 at 7:57 Comment(1)
See #53215538Mease
E
7

The client uses java.util.logging, the easiest way is to use SLF4J. See JUL to SLF4J Bridge for more information.

3 steps are required:

  1. Add jul-to-slf4j bridge to you classpath with runtime scope.
    Maven:

    <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>jul-to-slf4j</artifactId>
       <version>1.7.25</version>
       <scope>runtime</scope>
    </dependency>
    

Gradle:

`runtime group: 'org.slf4j', name: 'jul-to-slf4j', version: '1.7.25'`
  1. Add in logback.xml (logback-test.xml), don't forget to add a STDOUT appender (or change it accordingly)

    <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
        <resetJUL>true</resetJUL>
    </contextListener>
    
    <logger name="jdk.internal.httpclient.debug" level="debug" additivity="false">
        <appender-ref ref="STDOUT" />
    </logger>
    
    <root level="DEBUG">
        <appender-ref ref="STDOUT"/>
    </root>
    
  2. Add in your code:

     static {
     SLF4JBridgeHandler.removeHandlersForRootLogger();
     SLF4JBridgeHandler.install();
     }
    

Alternatively, for educational purposes it is possible to add a system property:
-Djdk.httpclient.HttpClient.log=all which will enable all logging printing to the console.

Erupt answered 17/2, 2019 at 7:57 Comment(2)
thank you this worked for me, after following ALL steps :)Oilla
Thanks @Maxim for this answer. However, this does log a lot of information. Is there a way to log only httpclient.wire.content.wire logs using this approach?Haroun
H
1

A bit of elaboration about logging details at HttpClient found in sources:

High-level logging

These logs are considered public and intended to track requests/responses made by HttpClient. By default these logs are turned off and may be turned on using system property jdk.httpclient.HttpClient.log. The value of the property is a comma-separated list of events to be logged. There is a special all value which denotes all events. More details about possible values could be found on javadoc since Java 20.

Logger names:

  • jdk.httpclient.HttpClient

Log level of messages: INFO

How to enable: pass -Djdk.httpclient.HttpClient.log=all to java application

Low-level logging

These logs are considered internal and intended to track internal machinery of HttpClient.

There is no need to specially enable these loggers using system properties or any other means. However it should be noted that HttpClient indeed recognize system properties with the same names and treats their values as booleans, i.e. -Djdk.internal.httpclient.debug=true. The effect of these boolean properties when set to true is that relevant log messages will be also printed to System.out/System.err irrespective of corresponding logger configuration.

Logger names:

  • jdk.internal.httpclient.debug
  • jdk.internal.httpclient.websocket.debug
  • jdk.internal.httpclient.hpack.debug

Log level of messages: DEBUG

How to enable: no special configuration

Honeyed answered 3/5 at 18:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.