my dog learned polymorphism*
The moose likes Other Open Source Projects and the fly likes log4j & log4j.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "log4j & log4j.xml" Watch "log4j & log4j.xml" New topic

log4j & log4j.xml

Hari patnaik

Joined: Apr 13, 2005
Posts: 12
Hi All

I have currently been using Log4j. I have all log4j related configuration in the file.
I have custom class called Logger which reads from this file doing an I/O operation.

Now we have to change this to using log4j.xml. I need all config properties in log4j.xml & a new java class to read from this properties from log4j.xml.

If anyone has already got a sample example or links where i can find the same please do post it.

I looked through a lot of sites for help but all use rather than log4j.xml.

Carol Enderlin
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Did you look at the log4j source code? You don't mention what you need to customize, not sure what you are trying to do or what to recommend beyond org.apache.log4j.xml.DOMConfigurator source.
Hari patnaik

Joined: Apr 13, 2005
Posts: 12
Hey Carol

Sorry if I could not state my problem scenario. Let me try to ellaborate this time.

I have been using & I want to convert it into log4j.xml.

Here is the file

#### Use two appenders, one to log to console, another to log to a file
log4j.rootCategory=debug, stdout, R

# Print only messages of priority WARN or higher for your category
# Specifically inherit the priority level

#### First appender writes to console

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

#### Second appender writes to a file

# Control the maximum log file size
# Archive log files (one backup file here)

log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n


Previously in a class called I had the following code to read the log4j configuration properties file


What approach should i follow to read log4j.xml file. Is the above code enuf or do I need to code a xmlreader to read log4j.xml file

Kindly respond

Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
I have log4j configure itself automatically - it just finds 'log4j.xml' at the 'root' of the classpath, and loads all its settings from there.

I've never found a good online source for log4j info. I'd buy a book on it, but I'm a student :-\ As a start, my log4j.xml currently looks like this:

Perhaps others can fill you in on how to initialise your system, but it shouldn't be too hard - and there must be a tutorial *somewhere*, I just haven't had to look

Carol Enderlin
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
If you use an xml file to define the configuration and want to call the equivalent configure method passing in the string file it's DomConfigurator.configure(filename).

You can do as Tim suggests and name the file log4j.xml, stick on the classpath, and log4j will find it. Or you can add this to your command if your file needs to be named something else:


See the short log4j manual.

FYI, The complete log4j manual by Ceki G�lc� is well worth the money it costs; it comes with a bunch of example code and explanations of the examples. I hope it gets updated for the new 1.3 version before my subscription expires :-)
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
After some struggle, I am finally able to 'log' stuff to a file.
I basically took Tim's XML file (Thanks Tim) and modified it to suit my web application. I am currently logging to D:\temp\junk.log.

I named the XML file as log4j.xml and copied it to my WEB-INF/lib and in the WEB-INF/classes directory. So, I am not sure which one is picked up. Its a trial and error process.

I haven't seen good documentation on how to set up the file etc etc untill I read this post. Thanks.

- m

Update: The log4j.xml file needs to be in the classes dir and not in the lib dir. When I put the file in the lib dir only, it did not create a logger and gave me an error message saying - no appender found.
So put it in the classes directory.

- m
[ July 12, 2005: Message edited by: Madhav Lakkapragada ]

Take a Minute, Donate an Hour, Change a Life
wood burning stoves
subject: log4j & log4j.xml
Similar Threads
remove log4j appender from an API
Need to do logging with my own with jboss 6.0!
Log4j: how do the loggers being added to repository? not found
Property file prob in Log4j