logback-access 를 이용하여 tomcat 의 HTTP access logs 남기기

The logback-access module, part of the standard logback distribution, integrates with Servlet containers such as Jetty or Tomcat to provide rich and powerful HTTP-access log functionality.

Logback was designed as a modular framework from the start. Making logback-core reusable under different circumstances without much recoding was one of our main goals. In accordance with this strategy, logback-access builds on top of logback-core and can thus provide much of the functionality of logback-classic but in the scope of HTTP-access logging.

It should be noted that while logback-access requires logback-core, it is independent of logback-classic as well as slf4j. Just as importantly, logback-access artifact must be installed at the container level, not at web-application level. It does not make sense to bundle logback-access.jar at the level of a web-application.

Step-by-step guide

logback-access 기능은 tomcat 7.x 를 지원하고 6.x 대에서 사용하려면 예전 버전의 logback 을 사용해야 한다. 

  1. logback 사이트에서 library 를 다운받는다. 
  2. 압축을 해제하고 logback-core-1.1.2.jar, logback-access-1.1.2.jar 파일을 TOMCAT의 lib 폴더에 복사한다.
  3. tomcat 의 conf/server.xml 파일에 Value 를 추가한다. Value는 Engine 이나 Host 태그 내에 있어야 한다.

    <Valve className="ch.qos.logback.access.tomcat.LogbackValve"/>
  4. LogbackValve  는 기본적으로 server.xml 이 있는 위치(conf)에서 설정파일(logback-access.xml) 을 찾는다.

 

logback-access.xml

기본 설정 파일

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

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%h %l %u %user %date "%r" %s %b</pattern>
    </encoder>
  </appender>

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

RollingFileAppender

encoder pattern 에 combined 라고 쓰면 '%h %l %u [%t] "%r" %s %b "%i{Referer}" "%i{User-Agent}"' 과 동일하다.

<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>http-access.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>http-access.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
    </rollingPolicy>

    <encoder>
      <pattern>combined</pattern>
    </encoder>
  </appender>
 
  <appender-ref ref="FILE" />
</configuration>

JMX 와 연계

 

 

Ref