Tomcat – logging all HTTP traffic with Logback

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)

<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)

      <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 &quot;%r&quot; %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)

<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>
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

4 Responses to Tomcat – logging all HTTP traffic with Logback

  1. Tom Pijl says:

    I assume this is tested on Tomcat 7, right?

  2. Charmi says:

    Hello By doing this should I be able to print e.printstacktrace() output to console?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s