作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以使用 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);
}
}
<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>
<root level="${rootLogLevel}">
<appender-ref ref="FILE"/>
</root>
关于properties - 是否必须创建多个 Logback PropertyDefiner 实现才能引入多个属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27390718/
我可以使用 Logback PropertyDefiner从 logback.xml 配置文件访问单个属性。如果我有 3 个要访问的属性,我目前正在使用 PropertyDefiner 的 3 个单独
我想在我的 logback.xml 配置文件中定义一些属性,并通过实现 PropertyDefiner 看到了。是以可定制的方式设置属性的好方法。 在开始实现它之后,我开始想知道如何访问标签内元素的名
我是一名优秀的程序员,十分优秀!