File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Open Source Projects and the fly likes New to log4j 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 "New to log4j" Watch "New to log4j" New topic
Author

New to log4j

K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
I want to add a logging (debug) facility to my Servlets and have come across log4j. It seems simple enough to use, but how does it all work? Is it just case of getting an instance of a Logger object and calling the debug() method? Can I have some sort of simple "-verbose" switch so I can turn the logging on and off (like with the java.exe -verbose option)? I know there is a properties file, but how can I setup it up to do this?

Thanks.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Yes, you get a Logger and then call Logger methods such as debug() or error()

Example basically from Log4j Manual:


A simple way to configure log4j is using a properties (or xml) file. The first time Logger.getLogger() is called log4j looks for either log4j.xml or log4j.properties on the classpath.

The manual also has sample properties files.



Then you said you wanted to turn off debug logging. Log4j uses Levels, if you set a logger's level to WARN, then it will log messages that are at level WARN or more severe (see manual for more or better explanation)

Suppose we are no longer interested in seeing the output of any component belonging to the com.foo package. The following configuration file shows one possible way of achieving this.

log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout

# Print the date in ISO 8601 format
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN


If you want a more in-depth manual than what is available at the URL I posted for log4j manual, try The complete log4j manual commercially ($) available in pdf.

[ March 16, 2005: Message edited by: Carol Enderlin ]
[ March 16, 2005: Message edited by: Carol Enderlin ]
 
Don't get me started about those stupid light bulbs.
 
subject: New to log4j
 
Similar Threads
jboss reliability
Log4J Help: Assign differen levels to a logger's appenders
how to retrace an unknown NullPointerException
oc4j, log4j and struts
J2EE and Logging