Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging Java web applications?

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?

like image 310
James McMahon Avatar asked Mar 05 '09 13:03

James McMahon


4 Answers

I would recommend you to use SLF4J. This is simple logging facade which supports most of popular logging systems (Log4j, commons-logging, Java Logging API and Logback). Using it, you will able to replace your underline logging system to any other, by simple CLASSPATH update.

The other benefit of SLF4J are parameterized calls, which reduces ugly logging code.

Actually, they recommends to use SLF4J with Logback. Logback is a successor of Log4J. And it was designed by the same author.

like image 148
Andrey Vityuk Avatar answered Sep 28 '22 00:09

Andrey Vityuk


Where does the configuration file go in .war package file? Root of the classpath.

Where do people log to, relative or absolute path flat file, or a database? Depends on the need. It's always better to use relative paths. Databases are good if you implement another application which will fetch logs from it and send them using email/sms

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. If you use Console appender, yes, it's going to be logged in your servlet container log file.

Any other gotchas I should be aware of for web application logging? If you are logging from different threads use logback, it's thread-safe and it exposes parameterized log messages.

like image 33
Boris Pavlović Avatar answered Sep 28 '22 02:09

Boris Pavlović


Logging to a DB adds another failure point. We had a situation where the prod servers logged to a DB and someone ran an expensive query on that DB that slowed it so much that the prod servers got very, very slow. Logging to a file can cause issues if you run out of space but it seems less likely to slow down the whole app.

like image 33
James A. N. Stauffer Avatar answered Sep 28 '22 02:09

James A. N. Stauffer


I place my configuration on the default package: src/

and log to files using the ${catalina.home} system property:

log4j.appender.???.file=${catalina.home}/logs/system.log
like image 36
cherouvim Avatar answered Sep 28 '22 02:09

cherouvim