In this tutorial you will learn how to install and run log4j.
Download the compressed log4j source, uncompress it, save the resulting log4j-1.2.4.jar at any desired location and include its absolute path in the application's CLASSPATH. Now, log4j API is accessible to user's application classes and can be used for logging.
To log an application class, follow these steps:
Import log4j package in the class.
Inside the class, instantiate a logger object using Logger.getLogger( ) static method.
Instantiate layouts (readymade or user-defined) to be assigned to appenders.
Instantiate appenders and assign desired layout to them by passing the layout object as parameter to their constructors.
Assign the instatiated appenders to the Logger object by invoking its addAppender( ) method with desired appender as parameter.
Invoke appropriate printing methods on Logger object to perform logging.
Steps 3, 4, and 5 can be skipped in case of external configuration.