File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other Open Source Projects and the fly likes or log4j.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark " or log4j.xml" Watch " or log4j.xml" New topic
Author or log4j.xml

pooja jain
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
which is good for a small application where i just want to log exceptions? or log4j.xml


Vinodkumar Beli

Joined: Nov 26, 2008
Posts: 4
keep this log where you want , but specify the path in Loader.getResource("c:/log4j.xml") correctly

1.<log4j:configuration xmlns:log4j="";>
2. <appender name="APPLOGFILE" class="org.apache.log4j.RollingFileAppender">
3. <param name="File" value="workshop_test.log" />
4. <param name="Append" value="true" />
5. <param name="MaxFileSize" value="3000KB" />
6. <layout class="org.apache.log4j.PatternLayout">
7. <param name="ConversionPattern" value="%d{DATE} %-5p %-15c{1}: %m%n"/>

8. <priority value="debug"></priority>
9. <appender-ref ref="File"/>

line 2 , here we are using RollingFileAppender . it will role the file if maximum file eceeds the size =3000kb
as mentioned in the line 5
line 3 to create file name called workshop_test.log where all logs are available in your root java_project
line 6 and 7 used for how the logs format to be logged on file .
important line 8. tells that from which level the log to printed .
levels like 1. trace 2.debug 4.warn 5. error 6. fatal. In production environment change the debug level to
error level to avoid unnessary log getting printed in file.

Load the log4j.xml use this code

import org.apache.log4j.helpers.Loader;
import org.apache.log4j.xml.DOMConfigurator;

/ use the same class loader as your class
URL url = LogClass.class.getResource("/");

// load custom XML configuration
URL url = Loader.getResource("c:/log4j.xml"); // if you keep it in c drive

Download the jar log4j-1.2.13.jar from site and build the jars to classpath using eclipse tool

Test your log4j set using this calss

import org.apache.log4j.Logger;

public class LogClass {
private static org.apache.log4j.Logger log = Logger

public static void main(String[] args) {




happy to help!!!
Vinodkumar Beli

Joined: Nov 26, 2008
Posts: 4
But if you use property file you cant able to set Filter in appender definition .

i.e<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="info"/>

LevelRangeFilter and LevelMatchFilters used to restrict of level to be logged to log4j file....
I agree. Here's the link:
subject: or log4j.xml
It's not a secret anymore!