• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Himai Minh

log4j.properties or log4j.xml

Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which is good for a small application where i just want to log exceptions? log4j.properties or log4j.xml

Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
keep this log where you want , but specify the path in Loader.getResource("c:/log4j.xml") correctly

1.<log4j:configuration xmlns:log4j="http://jakarta.apache.org/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 3.info 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("/my.properties");

// 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 docjar.com 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) {


Vinodkumar Beli
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'm full of tinier men! And a tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic