使用log4j2的Spring Boot.配置log4j2 Spring查找 [英] Spring boot with log4j2. Configure log4j2 Spring-lookup

查看:246
本文介绍了使用log4j2的Spring Boot.配置log4j2 Spring查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据log4j2文档:

According to log4j2 documentation:

Spring Boot Lookup从中检索Spring属性的值Spring配置.此查找将返回空值,直到Spring Boot初始化应用程序日志记录.

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>

此查找要求将log4j-spring-cloud-config-client包含在该应用程序.

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

配置这种查找的正确方法是什么?

What is the proper way to configure such a lookup?

我尝试组装以下应用程序:

I tried to assemble the following application:

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是否缺少一些依赖关系才能立即使用?

I expect the resolver to properly substitute "Demo" as the application name, however this does not happen. Does spring-boot-starter-log4j2 missing some dependencies for this to work out of the box ?

修改这是最简化的情况.实际上,我想将应用程序名称传递给gelf graylog附加程序.我想在多个组件中共享相同的日志记录配置.因此,解决方法(例如定义log4j属性或使用spring日志记录配置)不适合.

Edit This is the most simplified case. In reality, I want to pass the application-name to the gelf graylog appender. And I would like to share the same logging configuration within multiple components. So workarounds, like defining a log4j property, or using spring logging configuration, are not suitable.

推荐答案

更新14.05.2021:从log4j 2.14.0开始,有一个单独的模块用于弹簧查找.不幸的是,spring-boot仍然没有足够的log4j依赖关系.但是至少有人可以避免使用整个cloud-config-dependency.

Update 14.05.2021: Starting from log4j 2.14.0 there is a separate module for spring-lookup. Unfortunately, spring-boot still does not have high-enough dependency of log4j. But at least one can avoid using whole 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输入,我能够汇编最小配置以运行log4j2 Spring查找:

Based on @rgoers input I was able to assemble minimal configuration to run log4j2 Spring lookup:

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

此外,要禁用cloud-config(查找是必需的,但应用程序可能不需要):

Additionally, to disable cloud-config (which is required for look up, but might not be needed for the application):

bootstrap.yml(除了application.yml以外的其他文件)

spring.cloud.config.enabled: false

这篇关于使用log4j2的Spring Boot.配置log4j2 Spring查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆