views:

379

answers:

2

I'm currently logging via the simplest of methods within my servlet using Tomcat. I use the ServletConfig.getServletContext().log to record activity. This writes to the localhost.YYYY-MM-DD.log in $TOMCAT_HOME/logs.

I don't want to get away from the simplicity of this logging mechanism unless absolutely necessary. But I would like to name my log file. Rather than "localhost".YYYY-MM-DD.log, is there a way to have it write to "myAppName".YYYY-MM-DD.log. I know I could create my own mechanism, but again, I looking for simplicity here.

I'm hoping to stay away from a complete framework like Log4j.

A: 

For Tomcat 6.x, you can change the logging configuration in conf/logging.properties.

But I prefer a separate configuration with Log4j...

Hans Doggen
Is there a way to make the logging.properties specific to my servlet's context? Also, I would prefer simplicity, and Log4j does not strike me as simple (not necessarily difficult, but over burdened with options).
dacracot
log4j is very simple to use. Just take the 5 minutes necessary to set it up for the first time, use a same log4j.properties or log4j.xml configuration file, and you'll be very happy with the results (and wonder why you didn't use it before!).
matt b
A: 

So there is a way via the $TOMCAT_HOME/conf/logging.properties without touching my servlet code...

before...

handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, 3manager.org.apache.juli.FileHandler, 4admin.org.apache.juli.FileHandler, 5host-manager.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler

after...

handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, 3manager.org.apache.juli.FileHandler, 4admin.org.apache.juli.FileHandler, 5host-manager.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler, 6localhost.org.apache.juli.FileHandler

then add...

6localhost.org.apache.juli.FileHandler.level = FINE
6localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
6localhost.org.apache.juli.FileHandler.prefix = myAppName.

and add...

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/myAppName].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/myAppName].handlers = 6localhost.org.apache.juli.FileHandler

and bounce tomcat. This created a $TOMCAT_HOME/logs/myAppName.2008-10-14.log with my simplistic ServletConfig.getServletContext().log() messages only.

dacracot