This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "log4j & log4j.xml" Watch "log4j & log4j.xml" New topic
Author

log4j & log4j.xml

Hari patnaik
Greenhorn

Joined: Apr 13, 2005
Posts: 12
Hi All

I have currently been using Log4j. I have all log4j related configuration in the log4j.properties file.
I have custom class called Logger which reads from this log4j.properties 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 log4j.properties rather than log4j.xml.


Thanks
Hari
Carol Enderlin
drifter
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
Greenhorn

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 log4j.properties & I want to convert it into log4j.xml.

Here is the log4j.properties 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
log4j.category.your.category.name=WARN
# Specifically inherit the priority level
#log4j.category.your.category.name=INHERITED

#### First appender writes to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# 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
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=C:\\Test\\EJBModule\\resources\\logtest.log




# Control the maximum log file size
log4j.appender.R.MaxFileSize=100KB
# Archive log files (one backup file here)
log4j.appender.R.MaxBackupIndex=1

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

---------------------------------------------------------------------------

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

PropertyConfigurator.configure("C:\\Test\\EJBModule\\resources\\log4j.properties");
-----------------------------------------------------------------------


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

Bye
Hari
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


-Tim
Carol Enderlin
drifter
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:

-Dlog4j.configuration=foobar.xml

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
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: log4j & log4j.xml
 
Similar Threads
remove log4j appender from an API
Need to do logging with my own log4j.properties with jboss 6.0!
Log4j: how do the loggers being added to repository?
log4j.properties not found
Property file prob in Log4j