阅读logback文档,FileAppender相关内容
This commit is contained in:
@@ -511,4 +511,63 @@ FileAppender拥有如下property:
|
||||
- append: boolean类型,当文件已经存在时,如果设置为true,日志追加到文件末尾,如果日志设置为false,已存在文件的内容将会被清空。默认该属性被设置为true
|
||||
- encoder:Encoder类型
|
||||
- file:字符串类型,为写入文件的文件名,如果文件不存在,那么文件会被创建。如果该路径值中存在不存在的目录,FileAppender会自动创建目录。
|
||||
- bufferSize:FileSize类型,当immediateFlush属性被设置为false时,bufferSize选项将会设置output buffer的大小
|
||||
- bufferSize:FileSize类型,当immediateFlush属性被设置为false时,可以通过bufferSize选项将会设置output buffer的大小。bufferSize的默认值为8192。在定义FileSize类型时,可以按`KB, MB, GB`来指定,只需要以`5MB`形式指定即可。在没有指定后缀单位时,默认为字节
|
||||
- prudent: 当prudent属性设置为true时,会将`append`属性设置为true。prudent依赖与排他的file lock,在开启prudent后,写日志开销通常是prudent关闭开销的3倍。
|
||||
|
||||
> ##### Immediate flush
|
||||
> 默认情况下,每个log事件都会立即刷新到outputstream中,这样可以避免日志的丢失(如果日志刷新存在延迟,那么应用在未刷新的情况下当即可能会使缓存中的日志丢失)。
|
||||
>
|
||||
> 当时,如果要提高吞吐量,可以将`immediateFlush`property设置为false。
|
||||
|
||||
配置示例如下所示:
|
||||
```xml
|
||||
<configuration>
|
||||
|
||||
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
|
||||
<file>testFile.log</file>
|
||||
<append>true</append>
|
||||
<!-- set immediateFlush to false for much higher logging throughput -->
|
||||
<immediateFlush>true</immediateFlush>
|
||||
<!-- encoders are assigned the type
|
||||
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
|
||||
<encoder>
|
||||
<pattern>%-4relative [%thread] %-5level %logger{35} -%kvp- %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="DEBUG">
|
||||
<appender-ref ref="FILE" />
|
||||
</root>
|
||||
</configuration>
|
||||
```
|
||||
|
||||
##### `<timestamp>`
|
||||
对于短期应用,可能会期望在每次程序运行时,都创建唯一的日志文件。可以通过`<timestamp>`元素来实现该需求,示例如下:
|
||||
```xml
|
||||
<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="yyyyMMdd'T'HHmmss"/>
|
||||
|
||||
<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} -%kvp- %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="DEBUG">
|
||||
<appender-ref ref="FILE" />
|
||||
</root>
|
||||
</configuration>
|
||||
```
|
||||
|
||||
timestamp元素接收两个必填属性,`key`和`datePattern`,并接收一个非必填的属性`timeReference`。
|
||||
- datePattern : 格式和`SimpleDateFormat`相同
|
||||
- timeReference : 默认情况下,timestamp元素的值为当前配置文件被解析的事件,也可以将其设为`contextBirth`,即context创建时间
|
||||
|
||||
#### RollingFileAppender
|
||||
|
||||
Reference in New Issue
Block a user