简体   繁体   English

log4j2如何将属性变量从文件读取到log4j2

[英]log4j2 how to read property variable from file into log4j2

Background: As usual we have various life cycles like dev. 背景:像往常一样,我们有多种生命周期,例如dev。 stage, lt, prod all these are picked at deploy time from environment variable ${lifecycle}. 阶段,所有这些都是在部署时从环境变量$ {lifecycle}中选取的。 So JNDI setting we stores in ${lifecycle}.properties as variable datasource.jndi.name=jdbc/xxx. 因此,我们将$ {lifecycle} .properties中的JNDI设置存储为变量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. 由于其他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>

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. 如果不是使用Java系统属性,而是使用环境变量,则不应使用${sys:variable}前缀,而应使用${env:variable}前缀。 See also http://logging.apache.org/log4j/2.x/manual/lookups.html#EnvironmentLookup 另请参见http://logging.apache.org/log4j/2.x/manual/lookups.html#EnvironmentLookup

In general the placeholders that work in Spring bean configuration files do not work in Log4j configuration. 通常,在Spring bean配置文件中工作的占位符在Log4j配置中不工作。 They look the same, but the syntax and underlying discovery mechanism are completely different. 它们看起来相同,但是语法和基础发现机制完全不同。

For instance ${sys:something} attempts to resolve a Java system property. 例如, ${sys:something}尝试解析Java系统属性。 System properties are usually passed to JVM as command line arguments in format -Dkey=value and not stored in property files. 系统属性通常以-Dkey=value格式作为命令行参数传递给JVM,而不存储在属性文件中。

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. 您可以尝试使用资源束语法${bundle:MyProperties:MyKey}但是这将从该特定文件加载,并且不会执行任何其他Spring替换。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM