About Mohammad Meraj Zia

I did my Engineering in Information Technology from IET, Lucknow, India. Currently doing MSc in Information Technology from Derby University. I have worked in Java/J2EE domain for the last 10 years. Have good understanding of Payment and Finance domains.

Log4j Property Configuration Example

Log4J is an open source project which allows developers output log statements with configured granularity. The configuration can be maintained by the configuration files (XML, Property). Log4J has three main components: Loggers, Appenders and Layouts. These three components work together to help developers configure Log4J to output desired statements in given format and target. The benefit of log statements over simple System.out.print is that it allows you to define which statements gets output, and prevent some from not logging.

1. Loggers

Loggers follow the hierarchical naming rule which states that:

A logger is said to be an ancestor of another logger if its name followed by a dot is a prefix of the descendant logger name. A logger is said to be a parent of a child logger if there are no ancestors between itself and the descendant logger

The root logger resides at the top of the hierarchy and invoking the static Logger.getRootLogger method retrieves it. All other loggers are retrieve by their name: for e.g: if the logger name is MySampleLogger, it can be retrieve by calling Logger.getLogger(MySampleLogger.class);

2. Appender

Log4j allows logging requests to print to multiple destinations. An output destination is called an appender. Each Appender object has different properties associated with it, and these properties indicate the behavior of that object.

layout: Appender uses the Layout objects and the conversion pattern associated with them to format the logging information.

target – The target may be a console, a file, or another item depending on the appender.

level – The level is required to control how to filter of the log messages.

threshold – Appender can have a threshold level associated with it independent of the logger level. The Appender ignores any logging messages that have a level lower than the threshold level.

filter – The Filter objects can analyze logging information beyond level matching and decide whether logging requests should be handled by a particular Appender or ignored.

The output of a log statement of logger A will go to all the appenders in A and its ancestors. This is the meaning of the term “appender additivity“. However, if an ancestor of logger A, say B, has the additivity flag set to false, then A‘s output will be directed to all the appenders in A and its ancestors upto and including B but not the appenders in any of the ancestors of B. Loggers have their additivity flag set to true by default.

5. Property file

The log4j.properties file is a log4j configuration file which keeps properties in key-value pairs. By default, the LogManager looks for a file named log4j.properties in the CLASSPATH. Below is the property file used for configuring Log4J:

6. Debug Levels

Below is the description of the log levels used in log4j.

ALL – All levels including custom levels.DEBUG – Designates fine-grained informational events that are most useful to debug an application.ERROR – Designates error events that might still allow the application to continue running.FATAL – Designates very severe error events that will presumably lead the application to abort.INFO – Designates informational messages that highlight the progress of the application at coarse-grained level.OFF – The highest possible rank and is intended to turn off logging.TRACE – Designates finer-grained informational events than the DEBUG.WARN – Designates potentially harmful situations.

A log request of level L1 in a logger with level L2, is enabled if L1 >= L2. This rule is at the heart of log4j. It assumes that levels are ordered. For the standard levels, we have ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF.

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.