Close

Java Util Logging - Enabling JDK Internal Logging

[Last Updated: Nov 25, 2017]

To enable JDK internal logging, we need to simply assign a suitable logging level to the desired logger name. Following example shows how to enable HttpURLConnection internal logging.

Example

src/main/resources/logging.properties

handlers= java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = ALL
sun.net.www.protocol.http.HttpURLConnection.level=ALL

The main class

package com.logicbig.example;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class ExampleMain {

  static {
      String path = ExampleMain.class.getClassLoader()
                              .getResource("logging.properties")
                              .getFile();
      System.setProperty("java.util.logging.config.file", path);
  }

  public static void main(String[] args) throws Exception {
      URL url = new URL("http://www.example.com/");
      URLConnection yc = url.openConnection();
      try (BufferedReader in = new BufferedReader(new InputStreamReader(
              yc.getInputStream()))) {
          // do something
          // in.lines().forEach(System.out::println);
      }
  }
}

Output

Nov 25, 2017 8:20:23 PM sun.net.www.protocol.http.HttpURLConnection plainConnect0
FINEST: ProxySelector Request for http://www.example.com/
Nov 25, 2017 8:20:23 PM sun.net.www.protocol.http.HttpURLConnection plainConnect0
FINEST: Proxy used: DIRECT
Nov 25, 2017 8:20:23 PM sun.net.www.protocol.http.HttpURLConnection writeRequests
FINE: sun.net.www.MessageHeader@610455d65 pairs: {GET / HTTP/1.1: null}{User-Agent: Java/1.8.0_65}{Host: www.example.com}{Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2}{Connection: keep-alive}
Nov 25, 2017 8:20:23 PM sun.net.www.http.HttpClient logFinest
FINEST: KeepAlive stream used: http://www.example.com/
Nov 25, 2017 8:20:23 PM sun.net.www.protocol.http.HttpURLConnection getInputStream0
FINE: sun.net.www.MessageHeader@511d50c011 pairs: {null: HTTP/1.1 200 OK}{Cache-Control: max-age=604800}{Content-Type: text/html}{Date: Sun, 26 Nov 2017 02:20:24 GMT}{Etag: "359670651+ident"}{Expires: Sun, 03 Dec 2017 02:20:24 GMT}{Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT}{Server: ECS (ftw/FBA9)}{Vary: Accept-Encoding}{X-Cache: HIT}{Content-Length: 1270}

Example Project

Dependencies and Technologies Used:

  • JDK 1.8
  • Maven 3.3.9

JDK Internal Logging Select All Download
  • jdk-internal-logging
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ExampleMain.java
          • resources

    See Also