Spring配置文件,不同的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配置文件的解决方案:如何通过以下方式定义log4j2路径是application.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配置文件,不同的Log4j2配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!