Externalizing the logger and handler configuration to properties file

In all my previous posts under java logging, I have configured logger and handler programmatically.

We can externalize those configurations to properties file also. The advantage of this approach is that we can change configurations at any time without recompiling java class.

In this post I will show with example how to do that.

Let’s consider the below code

package logging;

import java.util.logging.Logger;

public class LoggingDemo11 {
    public static void main(String[] args) {
        Logger logger = Logger.getLogger("logging.LoggerDemo11");
        logger.info("Hello my name is Sumanth");

When you run the above code, it prints to console using “ConsoleHandler” and only those messages whose level is and above INFO.

But we didn’t told logger to use ConsoleHandler and neither did we told to log messages whose level is and above INFO then how did it come to know.

Well when the program runs it reads the properties file “logging.properties”, which is present in Java installation folder.

Below are the contents of the file.

1 handlers= java.util.logging.ConsoleHandler
2 .level= INFO
3 java.util.logging.FileHandler.pattern = %h/java%u.log
4 java.util.logging.FileHandler.limit = 50000
5 java.util.logging.FileHandler.count = 1
6 java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
7 java.util.logging.ConsoleHandler.level = INFO
8 java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

At line 1, the “handlers” property sets the global handler which is an instance of ConsoleHandler in this case. When you don’t specify an handler for your logger this handler will be used.

At line 2, we set the global log level. If you don’t specify log level for your logger and handler, this level will be used.

From line 3 to 5, we set certain properties of FileHandler and from line 6 to 8 we set certain properties for ConsoleHandler. Note we have used the fully qualified class name.

But how does the above java program knows the location of this file. The location to the java provided “logging.properties” file is set using the environment property “java.util.logging.config.file”.

We can change this environment property to point to the location where our custom properties file is there.

Lets see an example.

Below is the custom logging properties file I created with name “customlogger.properties”. The contents are as shown below

1 logging.LoggingDemo11.handlers=java.util.logging.FileHandler
2 logging.LoggingDemo11.level=INFO

3 java.util.logging.FileHandler.level=INFO
4 java.util.logging.FileHandler.pattern=File1.log

“logging.LoggingDemo11” is the fully classified class name and is the also the logger name that i will create in java class as shown below

Logger logger = Logger.getLogger("logging.LoggingDemo11");

In the above properties file, at line 1, I am setting the handler for my logger having the name “logging.LoggingDemo11” to use java provided FileHandler.

At line 2, I am setting the log level of my logger.

At line 3, I am setting the log level of my FileHandler

At line 4, I am setting the file name of my FileHandler

This properties file should be present in the classpath.

Next is the java code which will actually create the logger and use it. Below is the complete code

1  package logging;
3  import java.util.logging.Logger;
5  public class LoggingDemo11 {
6      public static void main(String[] args) {
7          System.setProperty("java.util.logging.config.file", "customlogger.properties");
8          Logger logger = Logger.getLogger("logging.LoggingDemo11");
9          logger.info("Hello my name is Sumanth");
10     }
11 }

In the above java code, at line 7 I am changing the value of environment property “java.util.logging.config.file” to point to “customlogger.properties” file. The JVM will search for this file first in the folder of my project. Add this file in your project folder.

At line 8, we create a Logger instance using the name “logging.LoggingDemo11”. It reads this logger properties from our custom properties file. All the properties (for eg handlers, level) belonging to this logger are prefixed by logger name “logging.LoggingDemo11”.

At line 9, we log the message.

This time it will create a file by name File1.log and add the message in that file.

Leave a Reply