Tomcat - logging all HTTP traffic with Logback

less than 1 minute read

1. Install logback in a tomcat\lib folder
logback-access-1.1.2.jar
logback-core-1.1.2.jar

2. Create a logback-access.xml in a tomcat\conf foler
Example)

1
2
3
4
5
6
7
8
9
10
11
12
<configuration>
  <appender name="cgFileRoller" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>c:/logs/logback.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>cg-logback.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
    </rollingPolicy>
    <encoder>
      <pattern>%fullRequest%n%n%fullResponse</pattern>
    </encoder>
  </appender>
  <appender-ref ref="cgFileRoller" />
</configuration>

3. Modify server.xml in a tomcat/conf folder
Example)

1
2
3
4
5
6
7
8
9
10
11
12
      <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->
        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log." suffix=".txt"/>
        <Valve className="ch.qos.logback.access.tomcat.LogbackValve"/>
</Host>

4. Modify web.xml to register filer
Example)

1
2
3
4
5
6
7
8
<filter>
  <filter-name>TeeFilter</filter-name>
  <filter-class>ch.qos.logback.access.servlet.TeeFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>TeeFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

Updated: