Table of Contents
Log4j is fast , reliable logging framework and can be easily integrated with the code. It is possible to enable logging at run time without using
There are two ways, you can do logging using log4j.
- log4j.properties
- log4j.xml
In this post, we will see how to configure using log4.xml
1) Create simple maven java project.
2)Â Put log4j entry in pom.xml .
1 2 3 4 5 6 7 |
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> |
3)Â Update maven project to download require jar
Right click on project-> Maven -> update project
4) Create log4j.xml file
Log to console:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" /> </layout> </appender> <root> <level value="DEBUG" /> <appender-ref ref="console" /> </root> </log4j:configuration> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="file" class="org.apache.log4j.RollingFileAppender"> <param name="append" value="false" /> <param name="maxFileSize" value="10MB" /> <param name="maxBackupIndex" value="5" /> <!-- For Tomcat --> <param name="file" value="appLogs.log" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" /> </layout> </appender> <root> <level value="DEBUG" /> <appender-ref ref="file" /> </root> </log4j:configuration> |
If you want to log on both to console and file, you can use below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" /> </layout> </appender> <appender name="file" class="org.apache.log4j.RollingFileAppender"> <param name="append" value="false" /> <param name="maxFileSize" value="10MB" /> <param name="maxBackupIndex" value="10" /> <param name="file" value="appLogs.log" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" /> </layout> </appender> <root> <level value="DEBUG" /> <appender-ref ref="console" /> <appender-ref ref="file" /> </root> </log4j:configuration> |
5) Sample class for Logger
Create a class called “LoggingMain.java” in src/main/java in package org.arpit.java2blog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; import org.apache.log4j.xml.DOMConfigurator; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; public class LoggingMain { private static final Logger logger = LogManager.getLogger(LoggingMain.class); public static void main(String[] args) { DOMConfigurator.configure("log4j.xml"); logger.info("Hello world"); logger.info("we are in logger info mode"); } } |
6)Â Run program:
When you run above program , you will get below output to console and file(appLogs.log)
1 2 3 4 |
0 [main] INFO org.arpit.java2blog.LoggingMain - Hellow world 1 [main] INFO org.arpit.java2blog.LoggingMain - we are in logger info mode |
Bingo!! we have successfully configured log4j using xml file in maven project.