如何通过application.properties定义log4j2路径? [英] How to define log4j2 path by application.properties?
问题描述
我希望根据当前活动的配置文件使用不同的 log4j2
日志目录。但它不起作用。
I want to have different log4j2
log directories based on the current active profile. But it does not work.
#application.properties:
spring.profiles.active=dev
log.path=d:/${spring.profiles.active}
#log4j2.xml:
<Properties>
<property name="path">${bundle:application:log.path}</property>
</Properties>
结果:在d上创建了一个文件夹:/叫 $ {spring。 profiles.active}
而不是解析为真正的spring配置文件名称。为什么?
Result: a folder is created on d:/ called ${spring.profiles.active}
instead of resolving to the real spring profile name. Why?
推荐答案
我解决了它如下:
log4j2.xml:
$ {main:spring.profiles.active}
MainMapLookup.setMainArguments(new String[] {"spring.profiles.active", "dev"});
SpringApplication.run(source, args);
您可以按如下方式获取vmargs,并在运行spring app之前动态设置配置文件:
ManagementFactory.getRuntimeMXBean()。getInputArguments()
You can get the vmargs as follows, and set the profile dynamically before running the spring app:
ManagementFactory.getRuntimeMXBean().getInputArguments()
或者甚至更好,多年后回到这个:
使用 $ {sys:spring.profiles.active}
,作为 -D
计为SystemProperties。在这种情况下,您不需要 MainMapLookup
。
Or even better, coming back to this after years:
use ${sys:spring.profiles.active}
, as any arguments given with -D
count as SystemProperties. You don't need the MainMapLookup
in this case.
这篇关于如何通过application.properties定义log4j2路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!