views:

81

answers:

1

I've read several questions here concerning Tomcat and logging but I still really don't understand the "bigger picture", hence my question:

How and where are my Webapps supposed to do their logging?

By default on my setup Tomcat 6.0.20 logs go in the following file/appender:

./apache-tomcat-6.0.20/logs/catalina.out

Am I suppose to have my webapps also log to this file/appender?

Let say my case is trivially simple and I've got just one servlet:

import ... // What do I import here in order to be able to log?

public class SOServlet extends HttpServlet {

    public void doGet(
        final HttpServletRequest request,
        final HttpServletResponse response
    ) throws IOException, ServletException {
    ... // I want to log here, what do I write?

What are the gotchas knowing that there are more than one webapp running on the same Tomcat? (apparently from reading the various questions there are many gotchas).

What about the .war, do I need to put log4j/sl4f/commons-logging/whatever in my .war?

For example I've read that, and I'm way more confused than before:

http://tomcat.apache.org/tomcat-6.0-doc/logging.html

+1  A: 

How and where are my Webapps supposed to do their logging?

It depends. You can either make use of ServletContext#log() (which in turn is available in servlets by the inherited getServletContext() method) to log it into the same logfile as the servletcontainer itself, or to go for a separate logger/logfile with help of under each slf4j and/or logback/log4j. The common practice is to give each webapp its own logger so that you have more freedom and control in logging.

For the case you're interested, here is a Tomcat wiki with regard to the subject.

What are the gotchas knowing that there are more than one webapp running on the same Tomcat? (apparently from reading the various questions there are many gotchas).

If this is with regard to logging: you'll end up with poor overview in the logfile and less freedom and control in logging the individual webapps.

What about the .war, do I need to put log4j/sl4f/commons-logging/whatever in my .war?

Regardless of the library used: If it is specific to the webapp, put it in the WAR. If it is common to all webapps, put it in appserver's common lib. If it is specific to the appserver, put it in the appserver's lib.

BalusC
@BalusC: +1 but... All these links are **scary**. They all points to doc and examples full of words like "it apparently works", "a better approach may be..." etc. I've tried *getServletContext()* but couldn't make it work yet. I'm amazed at the level of complexity involved for something that I thought would be trivial.
Webinator
Well, it's merely a matter of taste and "good practice". Nobody forbids you to use `ServletContext#log()`, but the normal practice is that you don't use it for the serious logging work. Wrt the `getServletContext()` part: it should be available if your servlet extends at least `GenericServlet` (`HttpServlet` does). Inside your `doGet()` method you ought to be able to just do `getServletContext().log("message");`.
BalusC
@BalusC: gotcha, as a quick 'hack' I'm using the getServletContext() approach which works fine for my 23 KB .war ;) I'll look into the cleaner approaches and commons-logging/juli/log4j later on :)
Webinator
You're welcome.
BalusC
slf4j does currently not have a servlet backend, so when needing to log to the container, slf4j is not very helpful.
Thorbjørn Ravn Andersen
@Thor: You're right, but I actually didn't mean it as a replacement of the servletcontext logger.
BalusC