Spring Profiles, different Log4j2 configs

后端 未结 6 1516
生来不讨喜
生来不讨喜 2020-12-05 19:58

In my application.yml I got:

logging: 
  config: classpath:log4j2.debug.yml

And some others in different profiles. When I start the Applica

6条回答
  •  庸人自扰
    2020-12-05 20:52

    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

提交回复
热议问题