gpt4 book ai didi

log4j - 如何为 log4j 属性指定默认值

转载 作者:行者123 更新时间:2023-12-04 20:44:37 26 4
gpt4 key购买 nike

我想通过更改系统属性来拥有一个可以在多个环境中运行的配置。下面是一个例子:

log4j.appender.file.File=/opt/apps/${myEnvironment}/log/MyService.log

但是,如果有人忘记设置系统属性,我想确保它仍然正常运行。有没有办法在属性文件中指定默认值?

最佳答案

那也许是可能的。根据 org.apache.log4j.PropertyConfigurator 的 API 文档,你的变量应该首先在系统属性中查找它的值,如果在那里找不到,它应该在配置文件中搜索。

All option values admit variable substitution. The syntax of variable substitution is similar to that of Unix shells. The string between an opening "${" and closing "}" is interpreted as a key. The value of the substituted variable can be defined as a system property or in the configuration file itself. The value of the key is first searched in the system properties, and if not found there, it is then searched in the configuration file being parsed. The corresponding value replaces the ${variableName} sequence. For example, if java.home system property is set to /home/xyz, then every occurrence of the sequence ${java.home} will be interpreted as /home/xyz.



https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html

不过,您必须验证配置文件中设置的变量是否会覆盖系统属性。

关于log4j - 如何为 log4j 属性指定默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19981590/

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