在类路径 application.property 上有 log4j2 和 spring 属性文件的标准配置。
log4j2.xml
<Properties>
...
<Property name="APP_LOG_ROOT">${bundle:application:log.root.dir}</Property>
...
</Properties>
应用程序属性
...
log.root.dir=/opt/tomcat/logs
...
数据已正确读入 log4j2.xml,但是如果我想在使用 Maven 创建工件时获得替代属性并放置不同的 application.property:
mvn clean install -Dapplication.properties.path=file:/some_path/application.properties
? 之后,我就可以正确读取新属性了。
@Value("${log.root.dir}")
private String ololo;
但 log4j2 无法自行完成此操作。
如果您想在 Log4j2 配置文件中使用 Spring Environment
中的any 值,则需要使用 Spring Boot Lookup:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-spring-boot</artifactId>
<scope>runtime</scope>
</dependency>
spring-boot
中。添加Spring Boot查找后,只需替换即可
${bundle:application:log.root.dir}
与
${spring:log.root.dir}
编辑:与作为应用程序的第一个系统之一启动的 Log4j 不同,Spring 的
Environment
仅在一段时间后才可用。
为了使查找工作:
Bojan的建议,将您的文件称为
log4j2-spring.xml
,<Select>
<SpringProfile name="default">
<Property name="APP_LOG_ROOT" value="${spring:log.root.dir}"/>
</SpringProfile>
<DefaultArbiter>
<Property name="APP_LOG_ROOT" value="...folder for initial logs..."/>
</DefaultArbiter>
</Select>