A colleague of mine asked me how to route logging output of Jakarta Tomcat 5.0 to Unix syslog. This is rather easy since tomcat uses a library called „jakarta commons logging“ which in turn can use „log4j“ which itself can log to Unix Syslog.

First, download log4j and commons-logging. Now copy log4j-1.2.x.jar and commons-logging.jar to tomcats common/lib directory. Create a file „log4j.properties“ in tomcats common/classes directory with the following content:

log4j.rootLogger=INFO,sysout,syslog

# sysout = Konsole
log4j.appender.sysout=org.apache.log4j.ConsoleAppender
log4j.appender.sysout.layout=org.apache.log4j.PatternLayout
log4j.appender.sysout.layout.ConversionPattern=%d{ISO8601} %-5p [%t] [%c] %m%n

####
# Configuration for syslog
##
# syslog = Syslog
log4j.appender.syslog=org.apache.log4j.net.SyslogAppender
# which Syslog-Facility is going to be used
# for valid values see
# http://logging.apache.org/log4j/docs/api/apache/log4j/net/SyslogAppender.html#getFacility(java.lang.String)
log4j.appender.syslog.facility=LOCAL0
log4j.appender.syslog.layout.ConversionPattern=%-5p [%t] [%c]: %m%n
log4j.appender.syslog.layout=org.apache.log4j.PatternLayout
# enter your host here
log4j.appender.syslog.SyslogHost=localhost

This log4j.properties configures log4j for writing to standard-output as well as to syslog-daemon, facility LOCAL0. You may change the logging format according to the documentation. If you want to disable logging to standard-output, remove „sysout“ from the first line in log4j.properties.

There are more tips as well as a list of Log-Identifiers in Tomcat 5.0.27 .

My colleague had one more wish: to redirect the access.log to syslog. This is more of a problem since the default tomcat implementation of access-logging (called AccessLogValve) uses direct file access instead of using common-loggings. Since it is open source, I took that implementation and replaced that file access to a common-loggings call. Download this tiny jar and place it in tomcats server/lib (not common/lib !!). Replace your usual „org.apache.catalina.valves.AccessLogValve“ with „de.epischel.tomcat.LoggingAccessLogValve“. The logging name „tomcat.accesslog“ and INFO-level is used and you can assign a different output destination (called appender) in log4j.properties:

log4j.additivity.tomcat.accesslog=false
log4j.logger.tomcat.accesslog=INFO,syslog2

where syslog2 may e.g. be a syslog appender like „syslog“ above but with a different facility or a different syslog-host.

Task done. 🙂

4 Gedanken zu „Tomcat and syslog and access.log

    • Hi Allen, I don’t have a version that is compatible with tomcat 5.5.26, sorry. I don’t work on this project any more, so I am not „up to date“.

  1. Hi,

    This setup doesn’t seem to work for logging configs that are set up under WEB-INF. Is there a way to make it work for this case as well?

    Your help is very much appreciated.

    Lance

Kommentare sind geschlossen.