Spring Profiles, different Log4j2 configs

后端 未结 6 1523
生来不讨喜
生来不讨喜 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:28

    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.

提交回复
热议问题