How to use Com6mons Logging
I’ve spent the whole of today trying to find information on how to properly configure commons logging in my java classes, specifically with Jdk14Logger. It’s been a labourous task as a) there’s not much out there and b) Apache’s documentation isn’t brilliant. I finally found one page on Tigris.org explaining commons logging (from the view point of a change in their software), which helped me greatly.
Due to the lack of decent documentation out there, I’m writing this article to try and help others.
Logging messages in your code
In order to log messages in your java code, you will need to import two classes into your source code. These classes are from the Apache Commons Logging library which can be downloaded from here.
Using the logger is pretty straight forward. You can send log messages by calling a method corresponding to priority:
Configuring your logger.
There are a number of ways you can set the properties of your logger, either in the system, in a file, in your java source code or at run time. I prefer in a file so that you have a record of your settings and they can be changed easily without chaning your source code every time you change a setting. In my example, I’ll have a file which sits in the same location as my jar file called commons-logging.properties.
The first setting tells the system what logger to use. To specify a specific logger be used, set:
So if the org.apache.commons.logging.Log property is not set and Log4J is found in the classpath, it will be used. Otherwise, if the JVM is version 1.4 or later, the java.util.logging classes will be used. Failing that, the built-in SimpleLog will be used.
To set up Log4JLogger:
To use Log4J, you need to configure a separate configuration file for it’s logging options. You will need to make sure that the Log4J binary (jar file) is in the classpath for the application. To use Log4J, you need to tell java you want to use it in your properties file:
To set up Jdk14Logger:
This is the information I struggled to find. To use the Jdk14Logger, set the Log property to:
The commons logging log levels correspond to the java.util.logging.Level levels like this:
So, if you want to set your class to log all debugs, you use LEVEL.FINE. These are also in order of priority, so, like in our example, if you set your class logger to FINE, then trace log messages will not be recorded. One other very important point to remember is that the ConsoleHandler defaults to a level of INFO, so in order to display debugs to the console (even if your logger is set to debug), you must set the level for the ConsoleHandler to debug. For example:
To set up SimpleLog:
Finally, if there are no other options for commons logging to use, it defaults to the SimpleLog implementation. This is an extremely simple logging system. To use it, use the property:
Running your program
In order to make sure your program uses the defined logger you’ve set up, run your java program like this:
So that java will load your config file and the logger you wish to use. If for some reason it does not want to pay attention to the Log property in your config file, add this line to your command:
And replace SimpleLog with whichever log implementation you wish to use.