2015-03-03 58 views
0

我們可以在appender級別設置ERROR和INFO級別嗎?你能爲同一個包提供兩個級別嗎

我使用logback.xml

我logback.xml

<configuration> 
     <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> 
      <encoder> 
       <pattern>%5p [%t] %m%n</pattern> 
      </encoder> 
      <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> 
       <level>DEBUG</level> 
      </filter> 
     </appender> 
     <appender name="default-out" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- Rolling file appender for rolling files --> 
      <param name="File" value="logs/app-track-log.log" /> 
      <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 
       <!-- daily rollover --> 
       <fileNamePattern>logs/app-track-log-%d{yyyy-MM-dd}.log</fileNamePattern> 

       <!-- keep 30 days' worth of history --> 
       <maxHistory>30</maxHistory> 
      </rollingPolicy> 
      <param name="encoding" value="UTF-8" /> 
      <encoder> 
       <pattern>%-30([web] %d{HH:mm:ss.SSS} [%thread]) %-5level %logger{32} - %msg%n</pattern> 
      </encoder> 
     </appender> 

     <appender name="error-out" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- Rolling file appender for rolling files --> 
      <param name="File" value="logs/error/app-error.log" /> 
      <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 
       <!-- daily rollover --> 
       <fileNamePattern>app-error.%d{yyyy-MM-dd}.log</fileNamePattern> 
       <!-- keep 30 days' worth of history --> 
       <maxHistory>30</maxHistory> 
      </rollingPolicy> 
      <param name="encoding" value="UTF-8" /> 
      <encoder> 
       <pattern>%-30([web] %d{HH:mm:ss.SSS} [%thread]) %-5level %logger{32} - %msg%n</pattern> 
      </encoder> 
     </appender> 
<!--here the logger to initiate the write operation --> 
     <logger name="org.company.controllers" level="ERROR" > 
      <appender-ref ref="error-out" /> 
     </logger> 

    <logger name="org.company.controllers" level="INFO" > 
    <appender-ref ref="default-out" /> 
    </logger> 

     <root level="OFF"> 
      <appender-ref ref="default-out" /> 
     </root> 
    </configuration> 

我可以知道附加器將執行寫入部件(輸出).logger將其重定向到正確的appender與它的屬性。

在我的情況下,我需要在不同的記錄器級別(INFO/ERROR)中使用相同的包來提供不同文件中的數據。

我的目標是在一個文件中寫入ERROR數據,在另一個文件中寫入INFO數據。

回答

0

你需要的是levelfilter在附加器,配置級過濾錯誤日誌

<appender name="default-out" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- Rolling file appender for rolling files --> 
      <param name="File" value="logs/app-track-log.log" /> 
      <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 
       <!-- daily rollover --> 
       <fileNamePattern>logs/app-track-log-%d{yyyy-MM-dd}.log</fileNamePattern> 

       <!-- keep 30 days' worth of history --> 
       <maxHistory>30</maxHistory> 
      </rollingPolicy> 
      <param name="encoding" value="UTF-8" /> 
      <filter class="ch.qos.logback.classic.filter.LevelFilter"> 
       <level>INFO</level> 
       <onMatch>ACCEPT</onMatch> 
       <onMismatch>DENY</onMismatch> 
      </filter> 
      <encoder> 
       <pattern>%-30([web] %d{HH:mm:ss.SSS} [%thread]) %-5level %logger{32} - %msg%n</pattern> 
      </encoder> 
     </appender> 

我假設你正在尋找只記錄以下

 <appender name="error-out" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- Rolling file appender for rolling files --> 
      <param name="File" value="logs/error/app-error.log" /> 
      <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 
       <!-- daily rollover --> 
       <fileNamePattern>app-error.%d{yyyy-MM-dd}.log</fileNamePattern> 
       <!-- keep 30 days' worth of history --> 
       <maxHistory>30</maxHistory> 
      </rollingPolicy> 
      <param name="encoding" value="UTF-8" /> 
      <filter class="ch.qos.logback.classic.filter.LevelFilter"> 
       <level>ERROR</level> 
       <onMatch>ACCEPT</onMatch> 
       <onMismatch>DENY</onMismatch> 
      </filter> 
      <encoder> 
       <pattern>%-30([web] %d{HH:mm:ss.SSS} [%thread]) %-5level %logger{32} - %msg%n</pattern> 
      </encoder> 
     </appender> 

而對於信息顯示錯誤出的appender信息和錯誤記錄在兩個單獨的文件中,否則您可能需要擴展過濾器。

相關問題