Search code examples
javatomcatloggingtomcat7logback

Logback config in tomcat


I'm running two web applications in a Tomcat 7.

The Tomcat has a logback-access.xml, which defines two appenders.

I can see that it's being picked up when starting tomcat, and the appender config is parsed correctly.

My question is, how do I let the webapp use the appenders defined in there?

I tried putting <logger> and <root> elements in the logback-access file, but that generates an error:

no applicable action for [logger], current ElementPath is [[configuration][logger]]

, and similar for root and appender-ref.

I tried putting a logback.xml file in my war files, both WEB-INF/classes and just WEB-INF, but it doesn't seem to be picked up.

So, how am I supposed to properly configure this?

The logback-access.xml in Tomcat:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <append>true</append>
        <file>${catalina.base}/logs/my=app.log</file>
        <encoder>
            <pattern>combined</pattern>
        </encoder>
    </appender>

    <appender name="STASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <destination>localhost:5044</destination>

        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
<!-- cut specific config -->
            </providers>
        </encoder>
    </appender>
</configuration>

The logback.xml in WEB-INF/classes:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <logger name="my.package" level="DEBUG"/>
    <logger name="com.amazonaws" level="INFO"/>
<!-- and several more loggers like this -->

    <root level="DEBUG">
        <appender-ref ref="FILE"/>
    </root>
    <root level="TRACE">
        <appender-ref ref="STASH"/>
    </root>
</configuration>

Solution

  • I'll just leave this here if someone has similar problems.

    First of all, you don't need a logback-access.xml or Valve in tomcat's server.xml at all. Log incoming requests using filters in your app.

    Then, if you need your appender(s) to be configured on the server instead of in the app, design a single logback.xml and put it in tomcat's lib folder (usually /usr/share/java/tomcat). You won't need a logback.xml in your app(s). The logback-common jar can be either in your war, or in the tomcat lib folder, whichever you prefer.

    This way, the app will find the generic logback.xml on the tomcat shared classpath, and use that as if it were included in the war.