Log4j XML Configuration Example (original) (raw)

In this example we will see how to configure Log4j using XML. You can use the property file as well but now days xml is preferred over property file. Note that unlike Log4j 1.x, the public Log4j 2 API does not expose methods to add, modify or remove appenders and filters or manipulate the configuration in any way.

In this example we are usign Java 1.6 and maven. We will create a simple HelloLog4J class with the main method which will call the LOGGER.debug() and LOGGER.info() methods.

1. Project Structure

The below image shows the project structure.

Figure 1. Project Structure

2. Java Class

Below is the HelloLog4J class which uses org.apache.log4j.Logger class to print the log data on console.

HelloLog4J.java

import org.apache.log4j.Logger;

/**

3. Log4j Configuration file

Below is the log4j.xml file.

log4j.xml

<log4j:configuration xmlns:log4j="" title="undefined" rel="noopener noreferrer">http://jakarta.apache.org/log4j/">

This will print all debug or higher messages to the console/screen.

3.1 Appender

The appender is defined first, with a name (in this case “_console_“). A layout is defined for the appender (in this case PatternLayout ), and a pattern is defined for the layout.

ConsoleAppender appends log events to System.out or System.err using a layout specified by the user. The default target is System.out.The other commonly used appender is org.apache.log4j.FileAppender. FileAppender appends log events to a file. Below is the example of FileAppender.

<appender name="fileAppender" class="org.apache.log4j.FileAppender">

                                          

File is the full path to the log file.

Append – ‘true’ to append the file, ‘false’ to truncate the file

The JMSAppender will publish logging information to a JMS Topic specified in the log4 configuration file

3.2 Filter Configuration

Filters can be defined at appender level. For example, to filter only certain levels, the LevelRangeFilter can be used like this:

SocketHubAppender sends LoggingEvent objects to a set remote a log servers, usually a SocketNode.
The SMTP Appender sends an email through SMTP for each logged message. The below configuration will email any log message that is an warning or higher:

4. Layout

There are different kinds of layouts used, for e.g:

The most commonly used layout is PatternLayout. There are some synchronization issues with this which are overcome by org.apache.log4j.EnhancedPatternLayout. This layout formats the logging event and return the result as String. The output depends on the conversion pattern. A conversion pattern is composed of literal text and format control expressions called conversion specifiers. You can insert any literal text in the conversion pattern. Each conversion specifier starts with a percent sign (%) and is followed by optional format modifiers and a conversion character. The conversion character specifies the type of data, e.g. category, priority, date, thread name. The format modifiers control such things as field width, padding, left and right justification.

SimpleLayout consists of the level of the log statement, followed by ” – ” and then the log message itself. For example,

INFO - Java Rocks!!!

5. Logger

The most important logger is the root logger. Other loggers inherit from the root, so if we don’t define any other logger all will use console appender. Note that by default Log4j assigns the root logger to Level.ERROR. Root logger resides at the top of the logger hierarchy. It is exceptional in two ways:

Invoking the class static Logger.getRootLogger method retrieves it. All other loggers are instantiated and retrieved with the class static Logger.getLogger method. This method takes the name of the desired logger as a parameter

6. Maven

Below is the pom.xml which defines the dependency

pom.xml

4.0.0

 <groupId>com.javacodegeek</groupId>
 <artifactId>log4j</artifactId>
 <version>1.0-SNAPSHOT</version>

 <dependencies>

     <dependency>
         <groupId>log4j</groupId>
         <artifactId>log4j</artifactId>
         <version>1.2.17</version>
     </dependency>

 </dependencies>

I have used IDEA IntelliJ IDE version 14.0 to develop this. You can use your choice of IDE. If you are using IntelliJ, right click on the HelloLog4J java file and click ‘Run HelloLog4J.main()’

Figure 2. Run Java Code

7. Output

Below is the result I got

Figure 3. Output

8. Download source code