Javatpoint Logo
Javatpoint Logo

Log4j XML

The two most important configuration options are using the log4j.xml configuration or using log4j.properties configuration. In this section, we will see how to configure log4j using XML. We can use the property file as well, but now a day's xml is preferred over property file.

This is the main configuration file having all runtime configurations used by log4j. This file will contain log4j appenders information, log level information and output file names for file appenders.

In the section of "Log4j Example", we explained how to configure log4j using the properties file. Here, everything remains the same except that we use the XML configuration file.

The following code shows the corresponding log4j.xml file:

To configure log4j using xml file, we use DOMConfigurator.configure() method.

Output:

log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Level value for root is  [DEBUG].
log4j: root level set to DEBUG
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: Adding appender named [console] to category [root].
2019-09-05 18:07:45 DEBUG HelloWorld:10 - Sample debug message
2019-09-05 18:07:45 INFO  HelloWorld:11 - Sample info message
2019-09-05 18:07:45 WARN  HelloWorld:12 - Sample warn message
2019-09-05 18:07:45 ERROR HelloWorld:13 - Sample error message
2019-09-05 18:07:45 FATAL HelloWorld:14 - Sample fatal message

The above example is for console output. If you want to get the output on file then you just need to change the log4j.xml file.

Output to File:

The below log4j.xml will redirect the logging to a file.

Output to Console and File:

Even you can get the output on console as well as file. The below log4j.xml configuration file redirect the output to both console and file.

Output:

log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Level value for root is  [DEBUG].
log4j: root level set to DEBUG
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: Adding appender named [console] to category [root].
log4j: Class name: [org.apache.log4j.RollingFileAppender]
log4j: Setting property [append] to [false].
log4j: Setting property [maxFileSize] to [10MB].
log4j: Setting property [maxBackupIndex] to [10].
log4j: Setting property [file] to [/logs/myStruts1App.log].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: setFile called: /logs/myStruts1App.log, false
log4j: setFile ended
log4j: Adding appender named [file] to category [root].
2019-09-05 18:26:30 DEBUG HelloWorld:10 - Sample debug message
2019-09-05 18:26:30 INFO  HelloWorld:11 - Sample info message
2019-09-05 18:26:30 WARN  HelloWorld:12 - Sample warn message
2019-09-05 18:26:30 ERROR HelloWorld:13 - Sample error message
2019-09-05 18:26:30 FATAL HelloWorld:14 - Sample fatal message





Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA