Java log4j2如何将文件中的属性变量读入log4j2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22460824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:51:26  来源:igfitidea点击:

log4j2 how to read property variable from file into log4j2

javaloggingjdbclog4jlog4j2

提问by CodeLab

Background: As usual we have various life cycles like dev. stage, lt, prod all these are picked at deploy time from environment variable ${lifecycle}. So JNDI setting we stores in ${lifecycle}.properties as variable datasource.jndi.name=jdbc/xxx. As other beans are also using this properties file, it is verified that such variable is loaded & file is in classpath, but somehow I am not able to consume this variable in log4j2.xml in below JDBC Appender.

背景:像往常一样,我们有各种各样的生命周期,比如 dev。stage, lt, prod 所有这些都是在部署时从环境变量 ${lifecycle} 中挑选出来的。因此,我们将 JNDI 设置存储在 ${lifecycle}.properties 中作为变量 datasource.jndi.name=jdbc/xxx。由于其他 bean 也在使用此属性文件,因此验证了此类变量已加载且文件位于类路径中,但不知何故我无法在 JDBC Appender 下方的 log4j2.xml 中使用此变量。

<JDBC name="DBAppender" tableName="V1_QUERY_LOG" bufferSize="4" ignoreExceptions="false">
    <DataSource jndiName="${sys:datasource.jndi.name}" />
    <Column name="event_date" isUnicode="false" isEventTimestamp="true" />
    <Column name="log_level" isUnicode="false" pattern="%level" />
    <Column name="logger" isUnicode="false" pattern="%logger" />
    <Column name="message" isUnicode="false" pattern="%message" />
    <Column name="exception_msg" isUnicode="false" pattern="%ex{full}" />
</JDBC>

I have tried some option like "${datasource.jndi.name}" too, or is there any way I can fit the solution in

我也尝试过一些像“${datasource.jndi.name}”这样的选项,或者有什么方法可以解决这个问题

<Properties>
 <Property name="datasource.jndi.name">get datasource.jndi.name from {lifecycle}.properties</property>
</Properties>

采纳答案by Remko Popma

If you are not using java system properties, but environment variables, you should not use the ${sys:variable}prefix, but the ${env:variable}prefix instead. See also http://logging.apache.org/log4j/2.x/manual/lookups.html#EnvironmentLookup

如果您使用的不是 java 系统属性,而是环境变量,则不应使用${sys:variable}前缀,而应使用${env:variable}前缀。另见http://logging.apache.org/log4j/2.x/manual/lookups.html#EnvironmentLookup

回答by anttix

In general the placeholders that work in Spring bean configuration files do not work in Log4j configuration. They look the same, but the syntax and underlying discovery mechanism are completely different.

通常,在 Spring bean 配置文件中工作的占位符在 Log4j 配置中不起作用。它们看起来相同,但语法和底层发现机制完全不同。

For instance ${sys:something}attempts to resolve a Java system property. System properties are usually passed to JVM as command line arguments in format -Dkey=valueand not stored in property files.

例如,${sys:something}尝试解析 Java 系统属性。系统属性通常作为命令行参数以格式传递给 JVM -Dkey=value,而不存储在属性文件中。

You can try to use Resource bundle syntax ${bundle:MyProperties:MyKey}however this will load from that specific file and will not perform any additional Spring substitutions.

您可以尝试使用 Resource bundle 语法,${bundle:MyProperties:MyKey}但这将从该特定文件加载,并且不会执行任何额外的 Spring 替换。

See also:

也可以看看: