CodesJava

Easy learning with example program codes

Log4j example using log4j xml file


We can also use an xml file named log4j.xml to configure the Log4j. Let us discuss the use of log4j.xml configuration file with the help of below example.

Example:

Log4jTest.java

import org.apache.log4j.Logger;
 
/**
 * This class is used to show the use of 
 * Log4j with the log4j.xml file.
 * @author codesjava
 */
public class Log4jTest {
	//Get the Logger object.
	private static Logger log = Logger.getLogger(Log4jTest.class);
 
	public static void main(String[] args) {	
		//logger messages
		log.debug("Log4j debug message test.");
		log.info("Log4j info message test.");
		log.warn("Log4j warn message test.");
		log.error("Log4j error message test.");
		log.fatal("Log4j fatal message test.");
	}
}

log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
	<appender name="CA" class="org.apache.log4j.ConsoleAppender">
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%m%n" />
		</layout>
	</appender>
	<root>
		<level value="info" />
		<appender-ref ref="CA" />
	</root>
</log4j:configuration>

Output:

Log4j info message test.
Log4j warn message test.
Log4j error message test.
Log4j fatal message test.

Download this example.
 
Next Topic: Logging levels.
Previous Topic: Log4j example using log4j properties file.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.

 

Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki