Win a copy of Emmy in the Key of Code this week in the General Computing 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

New to log4j

Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 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 package. The following configuration file shows one possible way of achieving this.

log4j.rootLogger=DEBUG, A1

# 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

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 ]
And inside of my fortune cookie was this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!