Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Tutorials Log4j log4j.xml Example

Related Tutorials


 
 

Share on Google+Share on Google+

log4j.xml Example

Advertisement
In earlier example you have seen that we can define properties of log4j logging file with the help of log4j.properties. Properties can also be defined within log4j.xml file. You have to add following contents to this XML file.

log4j.xml Example

     

In earlier example you have seen that we can define properties of log4j logging file with the help of log4j.properties. Properties can also be defined within log4j.xml file. You have to add following contents to this XML file.

 

 

 

 

 

 

log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
 <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
   <layout class="org.apache.log4j.PatternLayout">
     <param name="ConversionPattern" value="%d{ABSOLUTE} 
      %5p %c{1}:%L - %m%n"/>
	</layout>
     </appender>
       <root>
	  <priority value="debug"></priority>
	  <appender-ref ref="stdout"/>
	</root>
</log4j:configuration>

 Log4j looks for log4j.xml file first and then go for log4j.properties hence you must place both the files in your folder. We use log4j.xml since properties file does not provide some advanced configuration options such as Filter, some of ErrorHandlers, and few advanced Appenders.

log4j.properties

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=debug, stdout

Here is our SimpleLog class code:

SimpleLog.java  

import org.apache.log4j.*;
public class SimpleLog {
  static Logger logger = Logger.getLogger("SimpleLog.class");
  public static void main(String[] args) {
  logger.debug("Welcome to Log4j");  
  }
}

Output:

  13:07:40,875 DEBUG class:5 - Welcome to Log4j

Download Source Code

     

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 10, 2008

Related Tutorials

Discuss: log4j.xml Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
dinesh
November 26, 2012
different file location

I am using "PropertyConfigurator.configure(loggerfile);" for different file location for xml file this gives me error as below: log4j:WARN No appenders could be found for logger (log4j). log4j:WARN Please initialize the log4j system properly. any solution: thanks,,
DMCA.com