问题描述
在我的应用程序中.我得到了:
logging: config: classpath:log4j2.debug.yml
和其他一些不同的配置文件.启动应用程序时,我会得到以下内容:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
如果我只是将log4j2.xml放在其作品介绍的旁边.因此,我认为这是我错过依赖性的东西,或者log4j2不可能?
参考:启动记录级别说应该像我尝试一样可能吗?
推荐答案
在我这边,我正在使用属性文件而不是yaml.我想要两个日志文件:一个将所有内容都记录到控制台,另一个将一个记录在文件中.因此,我制作了两个log4j2配置文件:log4j2-dev.xml和log4j2-file.xml.
我使用两个弹簧配置文件:默认一个和一个名为" dev".要切换log4j2配置文件,我创建了一个文件应用程序.包含:
spring.profiles.active= logging.config=classpath:log4j2-file.xml
我还有另一个属性文件,application-dev.properties,当Spring检测" Dev"配置文件时,该文件会自动激活.它包含:
logging.config=classpath:log4j2-dev.xml
当我想使用log4j2-dev.xml配置时,我只将" dev"添加为" spring.profiles.active =" application.properties.
的值.您可以在此页面上查看Feiyu Zhou的答案.他提出了使用YAML配置文件的解决方案:如何通过如何定义log4j2路径application.properties?
当然,您始终可以删除application.properties和log4j2.xml中的log4j2-file.xml的属性loggging.config.默认情况下,Log4j2将加载它,而无需由Spring Profile触发
其他推荐答案
作为使用Spring配置文件的替代方法(要求您明确设置哪些配置文件是活动的),您可以使用Maven构建配置文件在Log4j2配置文件之间切换.
application.yml
logging: config: classpath:${log4j2.config}
pom.xml
<project> <properties> <log4j2.config>log4j2.xml</log4j2.config> </properties> <profiles> <profile> <id>local</id> <properties> <log4j2.config>log4j2-local.xml</log4j2.config> </properties> </profile> </profiles> </project>
这样,可以将默认的log4j配置文件(log4j2.xml)用于普通构建.
和第二个配置文件(log4j2-local.xml)可在使用local构建配置文件(例如mvn package -Plocal)构建项目时将其用于本地开发/测试.
其他推荐答案
这是Spring Boot 2和Log4J2.xml的解决方案 确保您有诸如application-local.properties,application-dev.properties之类的文件.
切勿将log4j2.xml保留在src/main/resources文件夹中 src/main/resources/local src/main/resources/dev等... 然后进行类似的条目 logging.config:classPath:local/log4j2.xml在应用程序local.properties中 和 logging.config:classPath:dev/log4j2.xml in Application-dev.properties
还将log4j2.xml保留在每个文件夹中,都使用相同的文件名log4j2.xml,并且再也不会在src/main/resources下保留一个文件,因为应用程序默认情况下会选择它,我们不需要它.如果这些不同的XML特定于您的环境,则具有不同的配置,并且应该可以使用..还可以使用提供的配置文件参数运行弹簧启动.
.然后,您可以根据以下环境修改日志文件路径...
<RollingFile name="RollingFileAppender" fileName="c:\\logs\\logs_test\\og4j2-demo.log" filePattern="c:\\logs\\logs_test\\log4j2-demo-%d{yyyy-MM-dd}-%i.log">
问题描述
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.
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?
推荐答案
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.
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
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
When I want to use the log4j2-dev.xml configuration, I just add "dev" as value of "spring.profiles.active=" in 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?
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
其他推荐答案
As an alternative to using Spring profiles (which requires you to explicitly set which profiles are active), you can use a Maven build profile to switch between log4j2 configuration files.
application.yml
logging: config: classpath:${log4j2.config}
pom.xml
<project> <properties> <log4j2.config>log4j2.xml</log4j2.config> </properties> <profiles> <profile> <id>local</id> <properties> <log4j2.config>log4j2-local.xml</log4j2.config> </properties> </profile> </profiles> </project>
In this way, a default log4j config file (log4j2.xml) can be used for normal builds.
And a second config file (log4j2-local.xml) can be used for local development/testing whenever the project is built with the local build profile (e.g. mvn package -Plocal).
其他推荐答案
Here is the solution that worked for me with spring boot 2 and log4j2.xml Make sure you have files like application-local.properties, application-dev.properties for different envs and profiles.
Never keep log4j2.xml in the src/main/resources folder instead it should be kept under profile specific folders created under src/main/resources as src/main/resources/local src/main/resources/dev etc... then make entries like logging.config: classpath:local/log4j2.xml in application-local.properties and logging.config: classpath:dev/log4j2.xml in application-dev.properties
also keep log4j2.xml in each of these folders with the same file name log4j2.xml and once again never keep one under src/main/resources since the application will pick it by default which we do not want. Have different configuration in each if these different xml specific to your environments and it should work.. Also run the spring boot with the profile argument supplied..
Then you can modify log file paths based on the environments as below...
<RollingFile name="RollingFileAppender" fileName="c:\\logs\\logs_test\\og4j2-demo.log" filePattern="c:\\logs\\logs_test\\log4j2-demo-%d{yyyy-MM-dd}-%i.log">