Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara or log4j.xml

Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which is good for a small application where i just want to log exceptions? or log4j.xml

Posts: 4
  • Mark post as helpful
  • send pies
  • 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="";>
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) {


Vinodkumar Beli
Posts: 4
  • Mark post as helpful
  • send pies
  • 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....
Goodbye moon men. Hello tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!