Log4j
by Abhijeet Kashnia
Apache's Log4j is a popular tracing API, and is particularly useful for logging code behavior.
There are 3 main classes involved : Logger, Appender and Layout.
Loggers:
Have a named hierarchy, with the RootLogger at the base.
Use the following static calls to get a logger.
public static Logger getRootLogger();
public static Logger getLogger(String name);
Every Logger may have a level, defined is the class log4j.Level.
TRACE,DEBUG,INFO,WARN,ERROR and FATAL.
and if unassigned it will inherit a level.
loggerObject.setLevel(Level.WARN);
loggerObject.warn("Your msg");
loggerObject.debug("Your msg");
(Check if ALL and OFF are also valid levels.)
A message is enabled according to its level compared with the logger's level.
Appender:
This is to redirect your log messages towards a destination, which can be say a FileAppender, ConsoleAppender, or an SMTPAppender for mailing.
Layout:
Every Appender needs to have an associated Layout, which is for formatting your output.
Configuration:
The logger's configuration can be done either through a properties file, or the more preferred and used XML file.
The apache Log4j's home page specified mirrors seems to be down, so try downloading the 2.3MB jar from somewhere else.
Take a look around, this background should be enough.
Advanced features:
How to optimize these logging statements so that the logging overhead is reduced.