gpt4 book ai didi

properties - 是否必须创建多个 Logback PropertyDefiner 实现才能引入多个属性?

转载 作者:行者123 更新时间:2023-12-04 17:34:28 27 4
gpt4 key购买 nike

我可以使用 Logback PropertyDefiner从 logback.xml 配置文件访问单个属性。如果我有 3 个要访问的属性,我目前正在使用 PropertyDefiner 的 3 个单独实现(每个属性一个)。

有没有办法从单个 PropertyDefiner 实现访问多个属性?或者也许还有另一个支持多个属性的接口(interface)?

我希望能够根据环境(dev、ist、uat、perf、prod)为各种日志记录配置(上下文名称、日志级别、appender 文件名、文件大小等)使用属性来插入不同的值。

我找到了this question ,类似,但没有回答如何访问多个属性的问题。

最佳答案

创建 PropertyDefiner实现类。 PropertyDefinerBase已经提供了实现。

package foo.bar;

import java.util.HashMap;
import java.util.Map;

import ch.qos.logback.core.PropertyDefinerBase;

public class LoggingPropertiesDefiner extends PropertyDefinerBase {

private static Map<String, String> properties = new HashMap<>();

static {
properties.put("encoderPattern", "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %message%n");
properties.put("maxFileSize", "50MB");
properties.put("rootLogLevel", "INFO");
}

private String propertyLookupKey;

public void setPropertyLookupKey(String propertyLookupKey) {
this.propertyLookupKey = propertyLookupKey;
}

@Override
public String getPropertyValue() {
//TODO In the real world, get properties from a properties loader.
return properties.get(propertyLookupKey);
}
}

在 logback.xml 中,为每个属性使用相同的 PropertyDefiner 类。为每个提供不同的查找键:
<define name="encoderPattern" class="foo.bar.LoggingPropertiesDefiner">
<propertyLookupKey>encoderPattern</propertyLookupKey>
</define>

<define name="maxFileSize" class="foo.bar.LoggingPropertiesDefiner">
<propertyLookupKey>maxFileSize</propertyLookupKey>
</define>

<define name="rootLogLevel" class="foo.bar.LoggingPropertiesDefiner">
<propertyLookupKey>rootLogLevel</propertyLookupKey>
</define>

引用 logback.xml 中的属性名称:
<root level="${rootLogLevel}">
<appender-ref ref="FILE"/>
</root>

关于properties - 是否必须创建多个 Logback PropertyDefiner 实现才能引入多个属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27390718/

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