gpt4 book ai didi

java - 在Logback自动扫描中配置日志记录操作

转载 作者:行者123 更新时间:2023-11-30 08:06:28 26 4
gpt4 key购买 nike

Logback 文档说,自动扫描与日志记录操作结合使用,即

the check whether the scan period has elapsed or not, is too costly in itself. In order to improve performance, ReconfigureOnChangeFilter is in reality "alive" only once every N logging operations. Depending on how often your application logs, the value of N can be modified on the fly by logback. By default N is 16, although it can go as high as 2^16 (= 65536) for CPU-intensive applications.

有没有办法在 xml 中配置“N”的值或通过任何其他方式。

最佳答案

快速浏览一下ch.qos.logback.classic.turbo.ReconfigureOnChangeFilter类中的源代码(logback 1.1.3)可以发现以下内容:

N 的起始值是硬编码的,如下所示:

  private volatile long mask = 0xF;

除非自己更改源代码并重新编译对此常量的更改,否则代码不提供任何类型的配置选项来更改此设置。

如何使用mask变量可以在这段代码中看到:

// for performance reasons, skip change detection (MASK-1) times out of MASK.
// Only once every MASK calls is change detection code executed
// Note that MASK is a variable itself.
if (((invocationCounter++) & mask) != mask) {
return FilterReply.NEUTRAL;
}

但是,正如文档所示,这个 mask (或 N)值由 logback 动态调整,因为它认为需要这样做才能保持良好的性能。

文档仅讨论在需要时向上调整N的值。但代码显示,当它感觉N的值太高时,也会动态地向下调整该值。

请注意以下代码,它确定如何动态调整 N 的值:

  // experiments indicate that even for CPU intensive applications with 200 or more threads MASK
// values in the order of 0xFFFF is appropriate
private static final int MAX_MASK = 0xFFFF;


// if less than MASK_INCREASE_THRESHOLD milliseconds elapse between invocations of updateMaskIfNecessary() method,
// then the mask should be increased
private static final long MASK_INCREASE_THRESHOLD = 100;

// if more than MASK_DECREASE_THRESHOLD milliseconds elapse between invocations of updateMaskIfNecessary() method,
// then the mask should be decreased
private static final long MASK_DECREASE_THRESHOLD = MASK_INCREASE_THRESHOLD*8;

// update the mask so as to execute change detection code about once every 100 to 8000 milliseconds.
private void updateMaskIfNecessary(long now) {
final long timeElapsedSinceLastMaskUpdateCheck = now - lastMaskCheck;
lastMaskCheck = now;
if (timeElapsedSinceLastMaskUpdateCheck < MASK_INCREASE_THRESHOLD && (mask < MAX_MASK)) {
mask = (mask << 1) | 1;
} else if (timeElapsedSinceLastMaskUpdateCheck > MASK_DECREASE_THRESHOLD) {
mask = mask >>> 2;
}
}

最后一个条件是说,如果最后一次检查是在超过 800 毫秒之前执行的,则 N 的值太高,需要向下调整。

假设您没有非常频繁的日志,那么您可以预期 mask 的值最终将降至 0,更改检测将不再进行跳过,它实际上会按照您的预期开始遵守您的扫描周期配置。

关于java - 在Logback自动扫描中配置日志记录操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31025990/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com