简体   繁体   中英

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>

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.

Logback-access has nothing to do with Logback in the application;

it has to be managed at Application Server level, by including the following jars ( docs ):

To use logback-access with Tomcat, after downloading the logback distribution, place the files logback-core-1.2.3.jar and logback-access-1.2.3.jar under $TOMCAT_HOME/lib/ directory, where $TOMCAT_HOME is the folder where you have installed Tomcat.

and configuring it with a different syntax from Logback which doesn't include any logger:

<configuration>
  <!-- always a good activate OnConsoleStatusListener -->
  <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />  

  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>access.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>access.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
    </rollingPolicy>

    <encoder>
      <pattern>combined</pattern>
    </encoder>
  </appender>

  <appender-ref ref="FILE" />
</configuration>

Does it work ? If it work, try to change it according to your needs for the FILE part.

For the STASH part, that's a different kettle of fish, since it is LogStash - not LogBack - and other integration issues may be involved in there.

Try achieving one step at a time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM