Spring 使用 log4j2 启动。配置log4j2 Spring-lookup

Spring boot with log4j2. Configure log4j2 Spring-lookup

根据 log4j2 文档:

The Spring Boot Lookup retrieves the values of Spring properties from the Spring configuration. This Lookup will return null values until Spring Boot initializes application logging.

<File name="Application" fileName="application.log">
  <PatternLayout>
    <pattern>%d %p %c{1.} [%t] $${spring:spring.application.name} %m%n</pattern>
  </PatternLayout>
</File>

This Lookup requires log4j-spring-cloud-config-client be included in the application.

配置此类查找的正确方法是什么?

我尝试assemble以下应用程序:

build.gradle

plugins {
    id 'org.springframework.boot' version '2.2.5.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

sourceCompatibility = '13'

repositories{
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'
}

configurations {
    all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
        exclude group: "ch.qos.logback", module: "logback-classic"
    }
}

主要

@SpringBootApplication
public class DemoApplication implements ApplicationRunner {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    private final Logger logger = LogManager.getLogger();

    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.debug("Debugging log");
        logger.info("Info log");
        logger.warn("Hey, This is a warning!");
        logger.error("Oops! We have an Error. OK");
        logger.fatal("Damn! Fatal error. Please fix me.");
    }
}

application.yml

spring.application.name: Demo

log4j-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
            <PatternLayout>
                <pattern>%d %p %c{1.} [%t] $${spring:spring.application.name} %m%n</pattern>
            </PatternLayout>
        </Console>
    </Appenders>

    <Loggers>
        <Root level="info">
            <AppenderRef ref="ConsoleAppender" />
        </Root>
    </Loggers>
</Configuration>

我希望解析器正确地将 "Demo" 替换为应用程序名称,但是这并没有发生。 spring-boot-starter-log4j2 是否缺少一些依赖项才能开箱即用?

编辑 这是最简化的情况。 实际上,我想将应用程序名称传递给 gelf graylog appender。 我想在多个组件中共享相同的日志记录配置。所以解决方法,比如定义 log4j 属性,或使用 spring 日志配置, 不适合。

Spring boot starter log4j2 几乎可以肯定是一个尚不支持 Spring 查找的版本。无需使用 Spring Boot Starter Log4j2,只需直接包含 2.13.0 或更高版本所需的 log4j 依赖项。

我建议您查看 https://github.com/apache/logging-log4j2/tree/log4j-2.13.1/log4j-spring-cloud-config/log4j-spring-cloud-config-samples/log4j-spring-cloud-config-sample-application 中的示例应用程序。我已经亲自测试了这个设置,并且在我工作的地方有一些基于它的项目。

2021 年 5 月 14 日更新: 从 log4j 2.14.0 开始,有一个单独的模块用于 spring-lookup。 不幸的是,spring-boot 仍然没有足够高的 log4j 依赖性。但至少可以避免使用整个 cloud-config-dependency。

implementation "org.apache.logging.log4j:log4j-api:${log4j2version}"
implementation "org.apache.logging.log4j:log4j-core:${log4j2version}"
implementation "org.apache.logging.log4j:log4j-spring-boot:${log4j2version}"

++++++++++++++++++++

根据@rgoers 的输入,我能够 assemble 最小配置到 运行 log4j2 Spring 查找:

build.gradle

plugins {
    id 'org.springframework.boot' version '2.2.5.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

sourceCompatibility = '13'

repositories{
    mavenCentral()
}

ext{
    log4j2version = '2.13.1'
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'
    
    implementation "org.apache.logging.log4j:log4j-spring-cloud-config-client:${log4j2version}"
    implementation "org.apache.logging.log4j:log4j-api:${log4j2version}"
    implementation "org.apache.logging.log4j:log4j-core:${log4j2version}"
}

configurations {
    all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
        exclude group: "ch.qos.logback", module: "logback-classic"
        exclude group: "org.springframework.cloud", module: "spring-cloud-bus"
    }
}

此外,禁用云配置(查找需要,但应用程序可能不需要):

bootstrap.yml(application.yml 之外的单独文件)

spring.cloud.config.enabled: false
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-spring-cloud-config-client</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-bus</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

我的log4j2.xml有

<RollingFile name="rollingFile"
             fileName="${logHome}/${spring:spring.application.name}.log">