logback prudent, SiftingAppender, layout, encoder的使用

本文介绍了Logback中prudent模式的使用方法及注意事项,该模式支持多JVM向同一日志文件安全写入。同时,文章还讲解了如何通过SiftingAppender实现不同线程的日志记录到不同的文件中,并对Encoder和Layout的配置进行了更新说明。
部署运行你感兴趣的模型镜像
[size=small][b]1. 将prudent设置为true 是为了支持多个JVM往同一个日志文件写日志.[/b]

参考[url]http://logback.qos.ch/manual/appenders.html#FileAppender[/url]里面的prudent描述

prudent
In prudent mode, FileAppender will safely write to the specified file, even in the presence of other FileAppender instances running in different JVMs, potentially running on different hosts. The default value for prudent mode is false.
Prudent mode can be used in conjunction with RollingFileAppender although some restrictions apply.

Prudent mode implies that append property is automatically set to true.

Prudent more relies on exclusive file locks. Experiments show that file locks approximately triple (x3) the cost of writing a logging event. On an "average" PC writing to a file located on a local hard disk, when prudent mode is off, it takes about 10 microseconds to write a single logging event. When prudent mode is on, it takes approximately 30 microseconds to output a single logging event. This translates to logging throughput of 100'000 events per second when prudent mode is off and approximately 33'000 events per second in prudent mode.

Prudent mode effectively serializes I/O operations between all JVMs writing to the same file. Thus, as the number of JVMs competing to access a file increases so will the delay incurred by each I/O operation. As long as the total number of I/O operations is in the order of 20 log requests per second, the impact on performance should be negligible. Applications generating 100 or more I/O operations per second can see an impact on performance and should avoid using prudent mode.

NETWORKED FILE LOCKS When the log file is located on a networked file system, the cost of prudent mode is even greater. Just as importantly, file locks over a networked file system can be sometimes strongly biased such that the process currently owning the lock immediately re-obtains the lock upon its release. Thus, while one process hogs the lock for the log file, other processes starve waiting for the lock to the point of appearing deadlocked.

The impact of prudent mode is highly dependent on network speed as well as the OS implementation details. We provide an very small application called FileLockSimulator which can help you simulate the behavior of prudent mode in your environment.


[b]2. SiftingAppender: logging different threads to different log files[/b]
[url]http://www.nurkiewicz.com/2013/04/siftingappender-logging-different.html[/url]


[b]Encoder and Layout-Pattern.[/b]
原文:[url]http://logback.qos.ch/codes.html#layoutInsteadOfEncoder[/url]
This appender no longer admits a layout as a sub-component, set an encoder instead.

As of logback version 0.9.19, the WriterAppender class has been renamed as OutputStreamAppender, with FileAppender now sub-classing the latter. OutputStreamAppender and sub-classes now take an Encoder as a sub-component instead of a Layout.

In practical terms, this means that configuration files need to be changed

from (DEPRECATED)

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>testFile.log</file>
...
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%msg%n</pattern>
</layout>
</appender>
or the shorter equivalent (DEPRECATED)

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>testFile.log</file>
...
<!-- layout are assigned the type ch.qos.logback.classic.PatternLayout by default -->
<layout>
<pattern>%msg%n</pattern>
</layout>
</appender>
to (GOOD)

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>testFile.log</file>
...
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%msg%n</pattern>
</encoder>
</appender>
or the shorter equivalent (GOOD)

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>testFile.log</file>
...
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
For layout type other than PatternLayout, for example HTMLLayout, your configuration files need to be changed

from (DEPRECATED)

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>testFile.log</file>
...
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%msg%n</pattern>
</layout>
</appender>
to (GOOD)

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>testFile.log</file>
...
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%msg%n</pattern>
</layout>
</encoder>
</appender>
We hope to make up for this inconvenience with cool new features which are only possible using encoders. During a transition period, layouts passed as parameter will be automatically wrapped by an encoder so that configuration files in the old format (using a layout instead of encoder) will continue to work unmodified.

[/size]

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

在 `logback.xml` 文件中,`<encoder>` 标签用于定义日志事件的格式化和编码方式,它决定了日志信息如何被转换为字节流并写入到输出目标(如文件、控制台等)。以下是关于 `<encoder>` 标签的详细解释: ### 标签属性 - **`class`**:指定编码器的具体实现类,默认值为 `ch.qos.logback.classic.encoder.PatternLayoutEncoder`,该类使用模式字符串来格式化日志事件。 ### 常见编码器及配置元素 #### 1. `PatternLayoutEncoder` 最常用的编码器,使用模式字符串来格式化日志信息。 ```xml <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>application.log</file> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!-- 日志输出格式 --> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> ``` - **`<pattern>`**:定义日志信息的输出格式。模式字符串由一系列的转换符组成,每个转换符以 `%` 开头,用于表示不同的日志信息。常见的转换符有: - `%d`:表示日期和时间,可通过 `{}` 指定日期格式,如 `%d{yyyy-MM-dd HH:mm:ss}`。 - `%thread`:表示线程名称。 - `%-5level`:表示日志级别,`-5` 表示左对齐,宽度为 5 个字符。 - `%logger{36}`:表示日志记录器的名称,`{36}` 表示缩写到 36 个字符。 - `%msg`:表示日志消息。 - `%n`:表示换行符。 #### 2. `JsonEncoder` 用于将日志事件编码为 JSON 格式,方便后续的日志分析和处理。 ```xml <appender name="JSON_FILE" class="ch.qos.logback.core.FileAppender"> <file>application.json</file> <encoder class="ch.qos.logback.contrib.json.classic.JsonEncoder"> <!-- 是否在每个 JSON 对象后添加换行符 --> <appendLineSeparator>true</appendLineSeparator> </encoder> </appender> ``` - **`<appendLineSeparator>`**:布尔值,指定是否在每个 JSON 对象后添加换行符,方便查看和处理。 #### 3. `NopEncoder` 不进行任何格式化,直接将日志事件的原始字节写入输出目标。 ```xml <appender name="RAW_FILE" class="ch.qos.logback.core.FileAppender"> <file>raw.log</file> <encoder class="ch.qos.logback.core.encoder.NopEncoder"/> </appender> ``` ### 自定义编码器 除了使用 Logback 提供的内置编码器,还可以自定义编码器,只需实现 `ch.qos.logback.core.encoder.Encoder` 接口。 ```java import ch.qos.logback.core.encoder.EncoderBase; public class CustomEncoder extends EncoderBase<Object> { @Override public byte[] headerBytes() { return null; } @Override public byte[] encode(Object event) { // 自定义编码逻辑 return event.toString().getBytes(); } @Override public byte[] footerBytes() { return null; } } ``` 在 `logback.xml` 中使用自定义编码器: ```xml <appender name="CUSTOM" class="ch.qos.logback.core.FileAppender"> <file>custom.log</file> <encoder class="com.example.CustomEncoder"/> </appender> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值