Spring Profiles, different Log4j2 configs
Asked Answered
D

6

22

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?

Dol answered 22/2, 2016 at 17:17 Comment(0)
C
51

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

Computation answered 1/8, 2016 at 14:19 Comment(1)
This doesn't answer the question, it only provides an alternative configuration set up. -1Breannebrear
S
6

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).

Spacing answered 21/5, 2018 at 13:11 Comment(0)
S
5

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..

env specific properties entry

run application for the env or profile needed

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"> 
Scoundrel answered 20/8, 2018 at 0:49 Comment(0)
F
4

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.

Feeler answered 1/8, 2016 at 23:0 Comment(0)
D
0

You application.yml seems to be correctly configured. Maybe the problem was because you need to add the dependencies jackson-databind and jackson-dataformat-yaml to your pom in order to use log4j2.yml.

In case you have a spring-boot-starter-web, jackson-databind is already imported, you only need jackson-dataformat-yaml:

<dependencies>
  <!-- not needed if you have spring-boot-starter-web in the classpath -->
  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
  </dependency>

  <dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-yaml</artifactId>
  </dependency>
</dependencies>
Dumpish answered 1/7, 2019 at 20:20 Comment(0)
A
0

I used xml files to configure log4j2. What worked for me was adding a @Component with a @PostConstruct method where I reconfigured the logging:

LoggerContext context = (LoggerContext)LogManager.getContext(false);
context.setConfigLocation(URI.create("path to file"));
context.reconfigure();

where the path to the file is something like classpath:log4j2-(insert spring profile here).xml. To do this, I had to exclude spring-boot-starter-logging and include instead spring-boot-starter-log4j2. I spent a long time trying to get this log4j2 configuration to work so maybe this will help someone else.

Aircraft answered 5/9, 2019 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.