Spring Profiles, different Log4j2 configs

Dennis Ich picture Dennis Ich · Feb 22, 2016 · Viewed 22.6k times · Source

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?

Answer

tlegrand picture tlegrand · Aug 1, 2016

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