logback prudent, SiftingAppender, layout, encoder的使用

Logback配置详解
本文介绍了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]
### 配置 Logback 使用不同日志布局模式 Logback 提供多种方式来定义日志输出格式,通过配置 `logback.xml` 文件可以实现这一点。主要通过 `<layout>` 或者更灵活的 `<encoder>` 来指定具体的日志记录器应该如何呈现消息。 #### 定义简单的 PatternLayout 布局 最常用的方法之一就是利用内置的 `PatternLayout` 类来自定义日志条目的外观。下面是一个基本的例子: ```xml <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <!-- encoder 默认使用 PatternLayout --> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <root level="debug"> <appender-ref ref="STDOUT" /> </root> </configuration> ``` 此配置会使得每一条日志都按照时间戳、线程名、级别、类路径以及实际的消息内容这样的顺序显示出来[^1]。 #### 自定义复杂的 Layout 实现 对于更加复杂的需求,则可以通过创建自定义的 layout 类来满足特定的应用场景需求。例如,在某些情况下可能希望对日志中的敏感信息进行遮罩处理。这可以通过继承 `ch.qos.logback.classic.PatternLayout` 并重写其中的部分方法完成,也可以像引用中那样直接采用现有的解决方案如 Baeldung 所提供的 `MaskingPatternLayout`[^2]。 ```xml <configuration> <appender name="MASKED_LOGS" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> <layout class="com.baeldung.logback.MaskingPatternLayout"> <pattern>%-5p [%d{ISO8601,UTC}] [%thread] %c: %m%n%rootException</pattern> </layout> </encoder> </appender> <root level="info"> <appender-ref ref="MASKED_LOGS"/> </root> </configuration> ``` 上述例子展示了如何应用带有屏蔽功能的日志布局,它不仅能够正常输出标准的日志信息,还能有效保护隐私数据不被泄露。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值