Spring Profiles, different Log4j2 configs

我是研究僧i 提交于 2019-11-29 02:27:16
tlegrand

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

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.

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

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!