Now it's time for the second library on this blog and the turn has come to Log4j. The log4j is a library used for logging in a Java program. The library has been highly distributed and there are many applications, both open source and proprietary that I have found using the library. The library is provided and maintained by the Apache foundation and the lib has been around for quite a while.
Levels
Log4j works with something called logging level and there are a number of levels, these are DEBUG, INFO, WARN, ERROR, FATAL. When a program that implements log4j runs it is set to log on a specific level and all messages that correspond to that and higher level (debug lowest, Fatal highest) will then be logged to the output.
Use
Log4j is easy to use and when you have defined your logger in a class like this:
static Logger logger = Logger.getLogger(My.class);the only thing you have to do to log something is call logger.info("Message"); This will make our program log a message on the level info.
Configuration and Appenders
What really strikes me when using this library is the powerful logging configuration that you can do after you actually have done your programming. When log4j is loaded it loads a configuration file that tells the library how you want it to log the output. To do this you add to your config something call appenders which are small applications that can take log4j output and log it to a medium, for an example there are appenders that can log to file, database, IM, email and so on.
In the configuration you are also able to configure on what level the appender should log. For a reference on how to configure the config file see the official log4j site.
In all of my Java projects Log4j is going to be sure companion due to its versatility in logging output and reconfigurability after the project has left the workshop.
/Fredrik
log4j - Libzter Article
log4j - Official site