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?
Configuration: the root element of a log4j2 configuration file; the status attribute represents the level at which internal log4j events should be logged. Appenders: this element contains a list of appenders; in our example, an appender corresponding to the System console is defined.
You can do this: %m{nolookups} in the layout. {nolookups} is how you set the property log4j2. formatMsgNoLookups=true within the configuration XML content.
Community support: Log4j 1. x is not actively maintained, whereas Log4j 2 has an active community where questions are answered, features are added and bugs are fixed. Automatically reload its configuration upon modification without losing log events while reconfiguring.
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">
See also the Log4j 2 Configuration manual page:
Log4j2 will first try to find a file called log4j2-test.yaml
in the classpath, then the same for JSON and XML, and finally will look for a file called log4j2.yml
in the classpath.
You can also specify the configuration file location explicitly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With