Close

Java Util Logging - Loading logging.properties

[Last Updated: May 18, 2017]

Java Util logging uses the configurations which are loaded through one of the options described here. In this tutorial, we will learn different ways to load logging.properties.

Loading logging.properties from absolute path

First let's get the default properties file (which comes with JDK), e.g. from here:
C:\Java\jdk1.8.0_111\jre\lib\logging.properties

Let's modify it for our application, we are adding logging level for our application base logger:

handlers= java.util.logging.ConsoleHandler
.level= INFO
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
com.logicbig.level = WARNING

Save the above file at local file system e.g. here d:\test-app\logging.properties

Let's use it in our example:

public class MyClass {

  private static Logger LOGGER;

  static {
      System.setProperty("java.util.logging.config.file",
              "d:\\test-app\\logging.properties");
      //must initialize loggers after setting above property
      LOGGER = Logger.getLogger(MyClass.class.getName());
  }

  public static void main(String[] args) {
      System.out.println("-- main method starts --");
      LOGGER.info("an info msg");
      LOGGER.warning("a warning msg");
      LOGGER.severe("a severe msg");
  }
}

Output

-- main method starts --
May 17, 2017 11:55:44 AM com.logicbig.example.MyClass main
WARNING: a warning msg
May 17, 2017 11:55:44 AM com.logicbig.example.MyClass main
SEVERE: a severe msg

Above output doesn't show INFO logs, that means our logging.properties setting of com.logicbig.level = WARNING is working as expected.

Note that in these examples, we are setting system properties programmatically, so that we can conveniently set different properties for different main classes in this demo. The right way is to use -DmyPro=value from command line or set it in our IDE run configuration.

Loading logging.properties from classpath

Let's save the properties file under resource folder:

src/main/resources/logging.properties

handlers= java.util.logging.ConsoleHandler
.level= INFO
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
com.logicbig.level = WARNING

The properties file cannot be directly loaded from classpath by using java.util.logging.config.file system property. We have following other options to accomplish that.

By getting absolute location of the classpath resource

public class MyClass2 {

  private static Logger LOGGER;

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

  public static void main(String[] args) {
      System.out.println("-- main method starts --");
      LOGGER.info("an info msg");
      LOGGER.warning("a warning msg");
      LOGGER.severe("a severe msg");
  }
}

Output

-- main method starts --
May 17, 2017 11:55:46 AM com.logicbig.example.MyClass2 main
WARNING: a warning msg
May 17, 2017 11:55:46 AM com.logicbig.example.MyClass2 main
SEVERE: a severe msg

By using LogManager#readConfiguration()

public class MyClass3 {
  private static Logger LOGGER;

  static {
      InputStream stream = MyClass3.class.getClassLoader().
              getResourceAsStream("logging.properties");
      try {
          LogManager.getLogManager().readConfiguration(stream);
      } catch (IOException e) {
          e.printStackTrace();
      }
      LOGGER = Logger.getLogger(MyClass3.class.getName());
  }

  public static void main(String[] args) {
      System.out.println("-- main method starts --");
      LOGGER.info("an info msg");
      LOGGER.warning("a warning msg");
      LOGGER.severe("a severe msg");
  }
}

Output

-- main method starts --
May 18, 2017 10:01:15 AM com.logicbig.example.MyClass3 main
WARNING: a warning msg
May 18, 2017 10:01:15 AM com.logicbig.example.MyClass3 main
SEVERE: a severe msg

Example Project

Dependencies and Technologies Used:

  • JDK 1.8
  • Maven 3.3.9

Loading Logging Props Select All Download
  • logging-prop-loading
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • MyClass.java
          • resources

    See Also