阅读logback文档,FileAppender相关内容

This commit is contained in:
asahi
2024-08-06 19:09:56 +08:00
parent 69f07de1f6
commit 0f5f9327b9

View File

@@ -511,4 +511,63 @@ FileAppender拥有如下property
- append: boolean类型当文件已经存在时如果设置为true日志追加到文件末尾如果日志设置为false已存在文件的内容将会被清空。默认该属性被设置为true - append: boolean类型当文件已经存在时如果设置为true日志追加到文件末尾如果日志设置为false已存在文件的内容将会被清空。默认该属性被设置为true
- encoderEncoder类型 - encoderEncoder类型
- file字符串类型为写入文件的文件名如果文件不存在那么文件会被创建。如果该路径值中存在不存在的目录FileAppender会自动创建目录。 - file字符串类型为写入文件的文件名如果文件不存在那么文件会被创建。如果该路径值中存在不存在的目录FileAppender会自动创建目录。
- bufferSizeFileSize类型当immediateFlush属性被设置为false时bufferSize选项将会设置output buffer的大小 - bufferSizeFileSize类型当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