使用Logback的不同日志文件

时间:2018-02-01 10:37:57

标签: java logback

我想将不同的日志级别记录到不同的文件中,除此之外,我还想为每个日志级别设置默认的consoleAppender。

因此输出应该是不同的日志级文件:

  1. 信息级文件
  2. 警告级别文件
  3. 错误级别文件
  4. 控制台也应始终保持启用状态。
  5.   <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
          <charset>UTF-8</charset>
          <pattern>%d{HH:mm:ss.SSS} [%thread] %highlight(%-5level) %cyan(%logger{35}) - %msg %n</pattern>
        </encoder>
      </appender>
    
        <!-- Logging settings for the info logs -->
      <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
          <level>INFO</level>
          <onMatch>ACCEPT</onMatch>
        </filter> 
        <file>logs\logFile.html</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
          <!-- daily rollover -->
          <fileNamePattern>logs\logFile.%d{yyyy-MM-dd}.%i.html</fileNamePattern>
          <timeBasedFileNamingAndTriggeringPolicy
              class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
              <!-- or whenever the file size reaches 50MB -->
            <maxFileSize>5MB</maxFileSize>
          </timeBasedFileNamingAndTriggeringPolicy>
          <!-- keep 60 days' worth of history -->
          <maxHistory>60</maxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
          <charset>UTF-8</charset>
          <layout class="ch.qos.logback.classic.html.HTMLLayout">
            <pattern>%d{HH:mm:ss.SSS}%thread%level%logger%line%msg</pattern>
          </layout>         
        </encoder>
      </appender>
    
      <!-- Logging settings for the error logs -->
      <appender name="FILEERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
      <filter class="ch.qos.logback.classic.filter.LevelFilter">
          <level>ERROR</level>
          <onMatch>ACCEPT</onMatch>
        </filter> 
        <file>logs\logFileError.html</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
          <!-- daily rollover -->
          <fileNamePattern>logs\logFileError.%d{yyyy-MM-dd}.%i.html</fileNamePattern>
          <timeBasedFileNamingAndTriggeringPolicy
              class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
              <!-- or whenever the file size reaches 50MB -->
            <maxFileSize>5MB</maxFileSize>
          </timeBasedFileNamingAndTriggeringPolicy>
          <!-- keep 60 days' worth of history -->
          <maxHistory>60</maxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
          <charset>UTF-8</charset>
          <layout class="ch.qos.logback.classic.html.HTMLLayout">
            <pattern>%d{HH:mm:ss.SSS}%thread%level%logger%line%msg</pattern>
          </layout>         
        </encoder>
      </appender>
    
      <!-- Logging settings for the warn logs -->
      <appender name="FILEWARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
      <filter class="ch.qos.logback.classic.filter.LevelFilter">
          <level>WARN</level>
          <onMatch>ACCEPT</onMatch>
        </filter> 
        <file>logs\logFileWarn.html</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
          <!-- daily rollover -->
          <fileNamePattern>logs\logFileWarn.%d{yyyy-MM-dd}.%i.html</fileNamePattern>
          <timeBasedFileNamingAndTriggeringPolicy
              class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
              <!-- or whenever the file size reaches 50MB -->
            <maxFileSize>5MB</maxFileSize>
          </timeBasedFileNamingAndTriggeringPolicy>
          <!-- keep 60 days' worth of history -->
          <maxHistory>60</maxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
          <charset>UTF-8</charset>
          <layout class="ch.qos.logback.classic.html.HTMLLayout">
            <pattern>%d{HH:mm:ss.SSS}%thread%level%logger%line%msg</pattern>
          </layout>         
        </encoder>
      </appender>   
    
      <root level="info">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />     
      </root>
    
      <root level="error">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILEERROR" />     
      </root>
    
      <root level="warn">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILEWARN" />     
      </root>
    
    </configuration>
    

    这里的问题是它在所有文件中写入每种类型的级别。

2 个答案:

答案 0 :(得分:4)

更新过滤器定义以包括:<onMismatch>DENY</onMismatch>,例如:

<filter class="ch.qos.logback.classic.filter.LevelFilter">
  <level>INFO</level>
  <onMatch>ACCEPT</onMatch>
  <onMismatch>DENY</onMismatch> 
</filter> 

onMismatch的默认行为是FilterReply.NEUTRAL,因此,如果不包含onMismatch声明,则您通过过滤器有效地允许所有条目是否与给定级别匹配。

您还应删除多个<root>声明,并将其替换为以下声明:

<root level="info">
  <appender-ref ref="STDOUT" />
  <appender-ref ref="FILE" />
  <appender-ref ref="FILEWARN" /> 
  <appender-ref ref="FILEERROR" />      
</root>

此声明将导致所有这四个appender运行,然后每个FILEFILEWARNFILEERROR appender中的过滤器将确保它们只处理日志事件正确的水平。

答案 1 :(得分:0)

这就是我所做的,而且它正在发挥作用。

read -p 'Song name: ' SONG
youtube-dl -x --audio-format mp3 "ytsearch:$SONG"