Spring 配置文件,不同的 Log4j2 配置

Spring Profiles, different Log4j2 configs

在我的 application.yml 我得到:

logging: 
  config: classpath:log4j2.debug.yml

还有其他一些不同的个人资料。当我启动应用程序时,我得到以下信息:

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

如果我只是将 log4j2.xml 放在配置文件旁边,它就可以工作。所以我认为这是我错过了依赖性或者 log4j2 不可能的东西?

参考:Boot Logging Level说应该可以像我试试?

在我这边,我使用的是属性文件而不是 yaml 文件。我想要两个日志文件:一个将所有内容记录到控制台,另一个记录文件。所以我做了两个log4j2配置文件:log4j2-dev.xml和log4j2-file.xml.

我使用两个 Spring 配置文件:默认一个和一个名为 "dev"。为了切换 log4j2 配置文件,我创建了一个文件 application.properties 包含:

spring.profiles.active=
logging.config=classpath:log4j2-file.xml

我还有另一个属性文件 application-dev.properties,它会在 Spring 检测到 "dev" 配置文件时自动激活。它包含:

logging.config=classpath:log4j2-dev.xml

当我想使用 log4j2-dev.xml 配置时,我只需在 application.properties.

中添加 "dev" 作为 "spring.profiles.active=" 的值

你可以在本页看看周飞宇的回答。他提出了一个使用 Yaml 配置文件的解决方案:

当然,您可以随时删除 application.properties 的属性 logging.config 并在 log4j2.xml 中重命名 log4j2-file.xml。它将由 Log4j2 默认加载,无需由 Spring 配置文件

触发

另见 Log4j 2 Configuration manual page:

Log4j2 将首先尝试在类路径中查找名为 log4j2-test.yaml 的文件,然后查找 JSON 和 XML,最后会查找名为 [=11] 的文件=] 在类路径中。

您也可以明确指定配置文件位置。

作为使用 Spring 配置文件(这需要您明确设置哪些配置文件处于活动状态)的替代方法,您可以使用 Maven 构建配置文件在 log4j2 配置文件之间切换。

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>

这样,默认的 log4j 配置文件 (log4j2.xml) 可以用于正常构建。

只要使用 local 构建配置文件(例如 mvn package -Plocal 构建项目,第二个配置文件 (log4j2-local.xml) 可用于本地 development/testing ).

这是对 spring boot 2 和 log4j2.xml 适用的解决方案 确保你有像 application-local.properties、application-dev.properties 这样的文件用于不同的环境和配置文件。

切勿将 log4j2.xml 保存在 src/main/resources 文件夹中,而应将其保存在 src/main/resources 下创建的配置文件特定文件夹下 src/main/resources/local src/main/resources/dev 等... 然后像这样输入 logging.config: 应用程序中的类路径:local/log4j2.xml-local.properties 和 logging.config: classpath:dev/log4j2.xml in application-dev.properties

同时将 log4j2.xml 保存在每个具有相同文件名的文件夹中 log4j2.xml 并且永远不要将一个保存在 src/main/resources 之下,因为应用程序将默认选择它,我们这样做不想。如果这些不同的 xml 特定于您的环境并且它应该可以工作,那么在每个中都有不同的配置。此外 运行 spring 使用提供的配置文件参数启动..

然后您可以根据环境修改日志文件路径如下...

    <RollingFile name="RollingFileAppender" fileName="c:\logs\logs_test\og4j2-demo.log"  filePattern="c:\logs\logs_test\log4j2-demo-%d{yyyy-MM-dd}-%i.log"> 

您 application.yml 似乎配置正确。也许问题是因为您需要将依赖项 jackson-databind 和 jackson-dataformat-yaml 添加到您的 pom 中才能使用 log4j2.yml.

如果您有 spring-boot-starter-web,jackson-databind 已经导入,您只需要 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>

我使用了 xml 个文件来配置 log4j2。对我有用的是添加一个 @Component 和一个 @PostConstruct 方法,我在其中重新配置了日志记录:

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

文件路径类似于 classpath:log4j2-(insert spring profile here).xml。为此,我必须排除 spring-boot-starter-logging 而包含 spring-boot-starter-log4j2。我花了很长时间试图让这个 log4j2 配置工作所以也许这会对其他人有所帮助。