Friday, February 4, 2011

As a follow up to Remco's How to set up Tomcat logging to Syslog, I am including my own version of a class that enables Tomcat logging to Syslog. This class overrides the standard AccessLogValve and logs messages via Log4J API to category accesslog, level INFO.

To compile this class, you will need catalina.jar and servlet-api.jar in your CLASSPATH.

To configure Tomcat to use this class, edit your conf/server.xml and replace className in the access logging Valve with com.ofc.tomcat.Log4JAccessLogValve. Then, in lib/log4j.properties, configure appenders for category accesslog (property log4j.logger.accesslog)


package com.ofc.tomcat;

/**
* Redirects AccessLogValve logging to Log4J category "accesslog" level info
*
* @author Nicholas Sushkin
* @version $Revision: 1.2 $ $Date: 2011/02/04 18:45:36 $
*/
public class Log4JAccessLogValve extends org.apache.catalina.valves.AccessLogValve
{
/**
* The descriptive information about this implementation.
*/
protected static final String info1 =
"com.ofc.tomcat.Log4JAccessLogValve/1.0";

private static final org.apache.log4j.Logger log =
org.apache.log4j.LogManager.getLogger("accesslog");

@Override
public void log(String message)
{
log.info(message);
}

@Override
public String getInfo()
{
return info1;
}

@Override
protected void open()
{
}
}