Alex Rivera | Logout

Logging Java web applications?

Asked 2009-03-05T13:03:48.303
22

I am planning to implement logging into a web application that I am currently working on but I am struggling with some of the details. What is the best way to go about logging a Java web application?

Specifically;

  • Where does the configuration file go in .war package file?
  • Where do people log to, relative or absolute path flat file, or a database?
  • Does Log4J logging go directly into the application server log file automatically or is that something you have to set up? In this case I am using Tomcat, but I often use Jrun.
  • Any other gotchas I should be aware of for web application logging?

Currently I am using Log4J but I imagine that the best practices would apply universally to all logging implementations.

EDIT:
One addition to the questions up top.

  • Where do you initilize the log configuration?

In a traditional app, I do this at the entry point;

DOMConfigurator.configureAndWatch("log4j.xml");

What would the web application equivalent be?

Edit
Report

1 Answer

0
  • Where does the configuration file go in .war package file?

Usually, I do not place any logging configuration into the application, rather leaving that to the appserver admins to configure logging server-wide. In the rare cases I want the log4j configuration deployed with a webapp, WEB-INF is the usual path.

  • Where do people log to, relative or absolute path flat file, or a database?

Again, depends on appserver settings. One common log file for a appserver and rotating on a daily basis is the usual setup. If there are any app-specific needs, the admin may configure a separate logfile for an app (distinguished by package / class names).

  • Does Log4J logging go directly into the application server log file automatically or is that something you have to set up? In this case I am using Tomcat, but I often use Jrun.

See above. For tomcat used for development purposes, I'd just look for its logging (log4j) configuration and add app-specific specific there.

  • Any other gotchas I should be aware of for web application logging? Performance. Limit the ,log level to a minimum (i.e. WARN or ERROR) once you go live. Use if (log.isDebugEnabled()) { log.debug("..."); } and alike constructs in your code.
answered 2009-03-05T14:56:50.340

Your Answer