• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

log4j & log4j.xml

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 :-)
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic