본문 바로가기
JAVA

Logback filter (slf4j)

by 아이티.파머 2019. 12. 9.
반응형

logback filter type

  • LevelFilter
    정확한 레벨 일치를 기반으로 이벤트를 필터링 한다. 이벤트 레벨이 구성된 레벨이 같으면 onMatch onMismatch 특성의 구성에 따라 필터가 이벤트를 승인하거나 거부 하게 할 수 있다.

<configuration>
  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
      <level>INFO</level>
      <onMatch>ACCEPT</onMatch>
      <onMismatch>DENY</onMismatch>
    </filter>
    <encoder>
      <pattern>
        %-4relative [%thread] %-5level %logger{30} - %msg%n
      </pattern>
    </encoder>
  </appender>
  <root level="DEBUG">
    <appender-ref ref="CONSOLE" />
  </root>
</configuration>

onMatch onMismatch 를 통해 로그에 남길것인지 선택 ACCEPT (승인), DENY(거절) 할 수 있다.

 

 

  • ThresholdFilter
    지정된 입계 값 아래 이벤트를 필터링 한다. 임계값보다 낮은 레벨의 경우 이벤트는 거부된다.

<configuration>
  <appender name="CONSOLE"
    class="ch.qos.logback.core.ConsoleAppender">
    <!-- deny all events with a level below INFO, that is TRACE and DEBUG -->
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <level>INFO</level>
    </filter>
    <encoder>
      <pattern>
        %-4relative [%thread] %-5level %logger{30} - %msg%n
      </pattern>
    </encoder>
  </appender>
  <root level="DEBUG">
    <appender-ref ref="CONSOLE" />
  </root>
</configuration>

 

 

  • EvaluatorFilter
    EventEvaluator를 캡슐화하는 일반 필터입니다.
<configuration>
    
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">      
      <evaluator class="ch.qos.logback.classic.boolex.GEventEvaluator"> 
        <expression>
           e.level.toInt() >= WARN.toInt() &amp;&amp;  <!-- Stands for && in XML -->
           !(e.mdc?.get("req.userAgent") =~ /Googlebot|msnbot|Yahoo/ )
        </expression>
      </evaluator>
      <OnMismatch>DENY</OnMismatch>
      <OnMatch>NEUTRAL</OnMatch>
    </filter>
    <encoder>
      <pattern>
        %-4relative [%thread] %-5level %logger - %msg%n
      </pattern>
    </encoder>
  </appender>

  <root level="DEBUG">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

 

 

  • TurboFilters
    터보 필터객체는 모두 TurboFilter 추상클래스를 확장하여 사용합니다. 일반 필터처럼 사용 한다.

 

사용중인 logback.xml 전문

기본 DAILY_ROLLING_FILE_APPENDER 에의해 로그가 기록되며 logback fitler 인 CRAWLER_BATCH_ERROR , FACEBOOK_LOG , GOOGLE_LOG 가 기록된다. DAILY_ROLLING_FILE_APPENDER 로그에는 logback filter가 포함된 모든 로그가 기록된다.

 

<?xml version="1.0" encoding="UTF-8"?>

<configuration scan="true" scanPeriod="30 seconds">

    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
    <include resource="org/springframework/boot/logging/logback/file-appender.xml"/>
    <!--
    /////////////////////////////////////////////////////////////////////////////////////////////////////
    // String Application 운영시 적용되는 logging 정책
    // Log Level 및 기타 설정의 중복되는 내역은 application.properties 에서 적용한 설정이 우선 적용된다.
    // application-*.yml
    /////////////////////////////////////////////////////////////////////////////////////////////////////
  -->

  <!-- ################################################ -->
  <!-- # 일반 롤링 파일 최대 30일 -->
  <!-- ################################################ -->
  <appender name="DAILY_ROLLING_FILE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <prudent>true</prudent>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>logs/%d{yyyy-MM-dd}/batch.crawler.log</fileNamePattern>
      <maxHistory>30</maxHistory>
    </rollingPolicy>
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <!--<level>INFO</level>
      <level>DEBUG</level>
      <level>ERROR</level>
      <level>WARN</level>-->
      <level>TRACE</level>
    </filter>
    <encoder>
      <Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
    </encoder>
  </appender>


  <!-- ################################################ -->
  <!-- # CRAWLER BATCH 일반 ERROR : 파일 최대 30일 			-->
  <!-- ################################################ -->
  <appender name="CRAWLER_BATCH_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>logs/error/error.log</File>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <FileNamePattern>logs/error/application.error.%d{yyyy-MM-dd}.log</FileNamePattern>
      <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder>
      <Pattern>%d{yyyy:MM:dd-HH:mm:ss} [%thread] %-5level %logger{35} - %msg %n</Pattern>
    </encoder>
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
      <level>error</level>
      <onMatch>ACCEPT</onMatch>
      <onMismatch>DENY</onMismatch>
    </filter>
  </appender>

  <!-- ################################################ -->
  <!-- # Filtering Logger Start      		    	          -->
  <!-- #    - 정책 : 30 유지 후 삭제  		    	          -->
  <!-- ################################################ -->
  <appender name="FACEBOOK_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>logs/facebook/facebook.log</File>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <FileNamePattern>logs/facebook/facebook_%d{yyyy-MM-dd_HH}.log.tar.gz</FileNamePattern>
      <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder>
      <Pattern>%d{yyyy:MM:dd-HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</Pattern>
    </encoder>
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
      <evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
        <marker>FACEBOOK_CRAWLER</marker>
      </evaluator>
      <OnMatch>ACCEPT</OnMatch>
      <OnMismatch>DENY</OnMismatch>
    </filter>
  </appender>

  <appender name="GOOGLE_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>logs/google/google.log</File>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <FileNamePattern>logs/google/google_%d{yyyy-MM-dd_HH}.log.tar.gz</FileNamePattern>
      <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder>
      <Pattern>%d{yyyy:MM:dd-HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</Pattern>
    </encoder>
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
      <evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
        <marker>GOOGLE_CRAWLER</marker>
      </evaluator>
      <OnMatch>ACCEPT</OnMatch>
      <OnMismatch>DENY</OnMismatch>
    </filter>
  </appender>

  <appender name="KAKAO_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>logs/kakao/kakao.log</File>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <FileNamePattern>logs/kakao/kakao_%d{yyyy-MM-dd_HH}.log.tar.gz</FileNamePattern>
      <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder>
      <Pattern>%d{yyyy:MM:dd-HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</Pattern>
    </encoder>
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
      <evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
        <marker>KAKAO_CRAWLER</marker>
      </evaluator>
      <OnMatch>ACCEPT</OnMatch>
      <OnMismatch>DENY</OnMismatch>
    </filter>
  </appender>

  <appender name="NAVER_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>logs/naver/naver.log</File>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <FileNamePattern>logs/naver/naver_%d{yyyy-MM-dd_HH}.log.tar.gz</FileNamePattern>
      <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder>
      <Pattern>%d{yyyy:MM:dd-HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</Pattern>
    </encoder>
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
      <evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
        <marker>NAVER_CRAWLER</marker>
      </evaluator>
      <OnMatch>ACCEPT</OnMatch>
      <OnMismatch>DENY</OnMismatch>
    </filter>
  </appender>

  <!--
  ///////////////////////////////////////////////////////////////////////
  // application properties 에 중복 적인 logging 정책이 설정이 되어 있으면
  // 우선순위로 application.properties 에 기입된 정책이 실행됨
  ///////////////////////////////////////////////////////////////////////
  -->

  <logger name="com.aereport" level="INFO"/>
  <logger name="sun.rmi" level="INFO"/>
  <logger name="reactor.ipc.netty" level="INFO"/>
  <logger name="org.springframework" level="INFO"/>


  <!-- ////////////////////////////////////////////////////////////////////////////////// -->
  <!-- //  Hibernate log config                                                           -->
  <!-- ////////////////////////////////////////////////////////////////////////////////// -->
  <logger name="org.hibernate" level="INFO"/>
  <!--<logger name="org.hibernate.type" level="TRACE"/>-->
  <!--<logger name="org.hibernate.type.BasicTypeRegistry" level="WARN"/>-->
  <!--<logger name="org.hibernate.type.descriptor.sql" level="TRACE"/>-->

  <!-- ////////////////////////////////////////////////////////////////////////////////// -->
  <!-- //  Netty & WebClient Http request log start                                       -->
  <!-- ////////////////////////////////////////////////////////////////////////////////// -->
  <logger name="io.netty" level="INFO" />

  <!-- Channel pipeline logging 설정 -->
  <!--<logger name="reactor.netty.channel.ChannelOperationsHandler" level="TRACE"/>-->
  <!--<logger name="org.springframework.web.reactive.function.client.ExchangeFunctions" level="TRACE"/>-->

  <!-- Google Adwords -->
  <logger name="com.google.api.ads.adwords.lib.utils.report_download" level="ERROR"/>

  <root level="INFO">
    <appender-ref ref="CONSOLE"/>
    <appender-ref ref="DAILY_ROLLING_FILE_APPENDER"/>
    <appender-ref ref="CRAWLER_BATCH_ERROR"/>
    <appender-ref ref="FACEBOOK_LOG"/>
    <appender-ref ref="GOOGLE_LOG"/>
  </root>
</configuration>

 

 

Slf4j infterface 참고 (퍼사드 패턴으로 java의 logging API 지원)

Simple Logging Facade for Java  

 

 

 

반응형