登录:配置一个特定的记录器以记录以下级别

时间:2018-11-01 17:59:50

标签: java logging logback

请考虑以下配置:

<configuration scan="true" scanPeriod="15 minutes">

  <property name="LOG_FILE_PATH" value="..." />
  <property name="DOCS_LOG_FILE_PATH" value="..." />

  <appender name="FILE"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_FILE_PATH}</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>${LOG_FILE_PATH}.%d</fileNamePattern>
      <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder>
      <pattern>%d{"HH:mm:ss,SSS"} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="DOCS_FILE"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
      <evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
        <marker>DOC</marker>
      </evaluator>
      <onMatch>ACCEPT</onMatch>
      <onMismatch>DENY</onMismatch>
    </filter>
    <file>${DOCS_LOG_FILE_PATH}</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
      <minIndex>1</minIndex>
      <maxIndex>5</maxIndex>
      <fileNamePattern>${DOCS_LOG_FILE_PATH}.%i</fileNamePattern>
    </rollingPolicy>
      <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <maxFileSize>10MB</maxFileSize>
    </triggeringPolicy>
    <encoder>
      <pattern>%d [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="INFO">
    <appender-ref ref="FILE" />
  </root>

  <!-- This level only be valid for this logger only -->
  <logger name="com.company.webapp" level="DEBUG" loggerOnly="true">
    <appender-ref ref="DOCS_FILE" />
  </logger>
  <logger name="org.springframework" level="WARN" />

</configuration>

请注意loggerOnly="true",其中我暗示记录器DOCS_FILE将接收从ERRORDEBUG的所有日志消息,而root将为此记录器记录日志仅INFO。这在某种程度上是ThresholdFilter的颠倒。

SO上的当前方法总是将级别提高,我不想将级别降低,但是自定义InvertedThresholdFilter甚至都没有收到该事件,因为未为DOCS_FILE启用该日志级别。 / p>

在保持配置可读性的同时,是否有合理的方法呢?

0 个答案:

没有答案