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.
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- private Log m_log = LogFactory.getLog(MyClass.class);
- private Log m_log = LogFactory.getLog("MyClassLogger");
Using the logger is pretty straight forward. You can send log messages by calling a method corresponding to priority:
- m_log.fatal(Object message);
- m_log.fatal(Object message, Throwable t);
- m_log.error(Object message);
- m_log.error(Object message, Throwable t);
- m_log.warn(Object message);
- m_log.warn(Object message, Throwable t);
- m_log.info(Object message);
- m_log.info(Object message, Throwable t);
- m_log.debug(Object message);
- m_log.debug(Object message, Throwable t);
- m_log.trace(Object message);
- m_log.trace(Object message, Throwable t);
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:
- org.apache.commons.logging.Log
- org.apache.commons.logging.impl.Log4JLogger
- org.apache.commons.logging.impl.Jdk14Logger
- org.apache.commons.logging.impl.SimpleLog
- org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
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:
- org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
- log4j.configuration=log4j.properties
To set up Jdk14Logger:
This is the information I struggled to find. To use the Jdk14Logger, set the Log property to:
- org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
- handler=java.util.logging.ConsoleHandler java.util.logging.FileHandler
- .level=INFO
- MyClassLogger.level=INFO
- # commons-logging.properties
- # jdk handlers
- handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
- # default log level
- .level=INFO
- # Specific logger level
- MyClassLogger.level=FINE
- # FileHandler options - can also be set to the ConsoleHandler
- # FileHandler level can be set to override the global level:
- #java.util.logging.FileHandler.level=WARN
- # log file name for the File Handler
- java.util.logging.FileHandler.pattern=javalog%u.log
- # Specify the style of output (simple or xml)
- java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
- # Optional - Limit the size of the file (in bytes)
- java.util.logging.FileHandler.limit=50000
- # Optional - The number of files to cycle through, by
- # appending an integer to the base file name:
- java.util.logging.FileHandler.count=1
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:
- java.util.logging.ConsoleHandler.level=FINE
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:
- org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
- # Default logging level for all instances of SimpleLog.
- # Must be either trace, debug, info, warn, error or fatal. Defaults to info.
- org.apache.commons.logging.simplelog.defaultlog=warn
- # Logging detail level for a SimpleLog instance named MyClassLogger.
- # Must be either trace, debug, info, warn, error or fatal. Defaults to the
- # above default if not set.
- org.apache.commons.logging.simplelog.log.MyClassLogger=debug
- # Show the log name in every message. Defaults to false.
- org.apache.commons.logging.simplelog.showlogname=true
- # Show the last component of the name to be output with every message. Defaults to true.
- org.apache.commons.logging.simplelog.showShortLogname=true
- # Show the date and time in every message. Defaults to false
- org.apache.commons.logging.simplelog.showdatetime=true
Running your program
In order to make sure your program uses the defined logger you’ve set up, run your java program like this:
Or
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.
沒有留言:
張貼留言