Spring Profiles,不同的 Log4j2 配置 [英] Spring Profiles, different Log4j2 configs
问题描述
在我的 application.yml 中我得到:
In my application.yml I got:
logging:
config: classpath:log4j2.debug.yml
还有其他一些在不同的配置文件中.当我启动应用程序时,我得到以下信息:
And some others in different profiles. When I start the Application I get the following:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
如果我只是将 log4j2.xml 放在配置文件旁边,它就可以工作.所以我认为这是我错过依赖的地方,或者 log4j2 不可能?
If I just put log4j2.xml next to the profiled ones it works. So I think this is a something where I miss a dependancy or it is not possible with log4j2?
参考:启动日志级别 说应该可以,就像我尝试的那样?
Reference: Boot Logging Level says it should be possible like I try?
推荐答案
在我这边,我使用的是属性文件而不是 yaml 文件.我想要两个日志文件:一个将所有内容记录到控制台,另一个记录在一个文件中.所以我做了两个log4j2配置文件:log4j2-dev.xml和log4j2-file.xml.
On my side, I am using properties file instead of a yaml one. I wanted two log files: one which logs everything to the console, and another one which logs in a file. So I made two log4j2 configuration files: log4j2-dev.xml and log4j2-file.xml.
我使用了两个 Spring 配置文件:默认的一个和一个名为dev"的配置文件.为了切换 log4j2 配置文件,我创建了一个文件 application.properties,其中包含:
I use two Spring profile: the default one and one named "dev". To switch the log4j2 configuration file, I created a file application.properties containing:
spring.profiles.active=
logging.config=classpath:log4j2-file.xml
我还有另一个属性文件 application-dev.properties,当 Spring 检测到dev"配置文件时,它会自动激活.它包含:
And I have another properties file, application-dev.properties, which is activated automatically when Spring detects the "dev" profile. It contains:
logging.config=classpath:log4j2-dev.xml
当我想使用 log4j2-dev.xml 配置时,我只需在 application.properties 中添加dev"作为spring.profiles.active="的值.
When I want to use the log4j2-dev.xml configuration, I just add "dev" as value of "spring.profiles.active=" in application.properties.
您可以在此页面查看周飞宇的回答.他提出了一个使用 Yaml 配置文件的解决方案:How to define log4j2 path byapplication.properties?
You can take a look to the Feiyu Zhou's answer at this page. He present a solution using a Yaml configuration file: How to define log4j2 path by application.properties?
当然,您可以随时删除 application.properties 的属性 logging.config 并在 log4j2.xml 中重命名 log4j2-file.xml.它将默认由 Log4j2 加载,无需由 Spring 配置文件触发
Of course, you could always remove the attribute logging.config of application.properties and rename log4j2-file.xml in log4j2.xml. It will be loaded by default by Log4j2 without the need to be triggered by a Spring profile
这篇关于Spring Profiles,不同的 Log4j2 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!