Chapter 四: Appenders

Chapter 4: Appenders

OutputStreamAppender

ConsoleAppender

三个可配置属性:

encoder:

target:  System.out or System.err,default is System.out

withJansi: ANSI color

 

 

<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
    </encoder>
  </appender>

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

 

 

FileAppender

The FileAppender, a subclass of OutputStreamAppender, appends log  events into a file.

可配置属性

append: 是否追加,默认为true

encoder:

file: 指定存放日志的文件,如果不存在,将自动创建(mkdirs)。

   文件路径:c:/test.log 或 c:\\test.log。文件属性没有默认值

prudent: true/false, default false. 确保在多JVM环境下的日志安全处理的机制(文件锁)。

   该机制如果被设置为true,则append属性将自动设置为true

   该机制会影响到日志写入效率!

 

<configuration>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>testFile.log</file>
    <append>true</append>
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender>
        
  <root level="DEBUG">
    <appender-ref ref="FILE" />
  </root>
</configuration>

 

 

Uniquely named files (by timestamp)

取配置文件被解析时的时间

<configuration>

  <!-- Insert the current time formatted as "yyyyMMdd'T'HHmmss" under
       the key "bySecond" into the logger context. This value will be
       available to all subsequent configuration elements. -->
  <timestamp key="bySecond" datePattern="yyyy年MM月dd日HH时mm分ss秒"/>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <!-- use the previously created timestamp to create a uniquely
         named log file -->
    <file>log-${bySecond}.txt</file>
    <encoder>
      <pattern>%logger{35} - %msg%n</pattern>
    </encoder>
  </appender>

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

 

使用系统的contextBirth 设置时间

<configuration>
  <timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss" 
             timeReference="contextBirth"/>
  ...
</configuration>

  

 

RollingFileAppender

滚动日志

两个重要的组件:

   RollingPolicy     负责日志滚动-how

   TriggeringPolicy  负责触发日志回滚动作的执行-when

RollingFileAppender 必须配备RollingPolicy 和   TriggeringPolicy

不过,RollingPolicy 已经对TriggeringPolicy 进行了实现,因此,只需要配置RollingPolicy 即可!

 

RollingFileAppender可配置属性:

file:记录日志的文件,该属性可选。如果没有设置,logback将自动进行文件命名。

如logFile.2014-03-07.log。

但是,通过file属性指定文件,可以实现已记录日志与正在记录日志文件的解耦。

append:是否追加,默认true

encoder:

rollingPolicy:负责日志滚动时的具体行为

triggeringPolicy:告诉fileAppender何时激活日志的滚动行为

prudent:FixedWindowRollingPolicy 不支持

TimeBasedRollingPolicy 进行联合使用,不支持也不允许文件压缩,file属性不能设置,必须为blank

 

 

TimeBasedRollingPolicy

TimeBasedTriggeringPolicy 实现了RollingPolicy和TriggeringPolicy

TriggeringPolicy具有1个强制属性和几个可选属性:

fileNamePattern 强制属性,指定日志文件的名称样式(actived,已记录完日志的文件)

%d{pattern} SimpleDateFormat格式,默认为yyyy-MM-dd

注意:"/" 或者 "\",logback将其解析为路径分隔符

maxHistory int类型 指定最大保留多少个文件,将自动删除最旧的文件

cleanHistoryOnStart boolean类型 在appender启动的时候移除已有日志文件?

 

fileNamePattern详细配置  

/wombat/foo.%d   每天自动滚动[daily rollover at midnight]

没有设置file属性

第1天的日志名为/wombat/foo.2006-11-23

第2天的日志名为/wombat/foo.2006-11-24

设置了file属性,即指定了file名称

                设置了日志文件名称为/wombat/foo.txt

到午夜,foo.txt将被重命名为/wombat/foo.2006-11-23

然后,logback将创建1个新的foo.txt日志文件(保证当天记录日志的文件名始终都是foo.txt)

 

/wombat/%d{yyyy/MM}/foo.txt  每个月初进行滚动

file property not set:

当月:    /wombat/2006/10/foo.txt

 下个月:/wombat/2006/11/foo.txt.         

file property set to /wombat/foo.txt:

  在2006年10月份,日志名称始终是 /wombat/foo.txt

  在2006年11月份,日志名称将被重命名为 /wombat/2006/10/foo.txt

  而且,1个新的日志文件被创建,名称仍未指定的文件名/wombat/foo.txt

 到在2006年12月份,又被重命名为/wombat/2006/11/foo.txt

然后再创建1个新的/wombat/foo.txt

 

/wombat/foo.%d{yyyy-ww}.log  每周滚动1次

 

/wombat/foo%d{yyyy-MM-dd_HH-mm}.log  每分钟滚动1次

 

/foo/%d{yyyy-MM,aux}/%d.log  带文件夹的每天滚动1次 【aux 辅助标记】

日志将被放在1个以年和月命名的文件夹下,而且日志名称采用年月日的方式命名

由于%d没有aux修饰,将被作为日志的命名规则

这样,日志名称按%d进行命名,同时,文件夹将以年和月进行命名

如,folder :   /foo/2006-11/

日志路径则为: /foo/2006-11/2006-11-14.log

 好处:非常方便的将日志文件放在不同文件夹下进行管理!

 

/wombat/foo.%d.gz  在日志滚动的时候,将对历史日志文件进行压缩

如果,fileNamePattern以".gz"或者".zip"结尾,日志压缩功能将被开启

 

<configuration>
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>logFile.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- daily rollover -->
      <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>

      <!-- keep 30 days' worth of history -->
      <maxHistory>30</maxHistory>
    </rollingPolicy>

    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender> 

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

 

<configuration>
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <!-- Support multiple-JVM writing to the same log file -->
    <prudent>true</prudent>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
      <maxHistory>30</maxHistory> 
    </rollingPolicy>

    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender> 

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