kafka附加程序中log4j2.xml的配置更改?

时间:2018-08-17 02:44:05

标签: spring-boot spring-cloud log4j2 spring-kafka spring-cloud-config

我正在使用log4j2 kafka附加程序,log4j2.xml看起来像

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" name="spring-boot-log-to-kafka-example" packages="com.ms.lib.infrastructure.eurekaservice">
    <Appenders>
        <Kafka name="kafkaAppender" topic="0v8uyhvb-logs">
            <JSONLayout />
            <Property name="bootstrap.servers">xyz.com:9094,xyz.com:9094,xyz.com:9094</Property>
            <Property name="security.protocol">SASL_SSL</Property>
            <Property name="sasl.mechanism">SCRAM-SHA-256</Property>
            <Property name="sasl.jaas.config">org.apache.kafka.common.security.scram.ScramLoginModule required username="uname" password="pwd-gkB8qv4P";
            </Property>
        </Kafka>

        <!--stdout/stderr included for testing Oozie log4j edits-->
        <Console name="stdout" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5p [%-7t] %F:%L - %m%n"/>
        </Console>
        <Console name="stderr" target="SYSTEM_ERR">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5p [%-7t] %F:%L - %m%n" />
            <Filters>
                <ThresholdFilter level="WARN" onMatch="ACCEPT" />
            </Filters>
        </Console>

    </Appenders>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="kafkaAppender"/>

            <!--stdout/stderr included for testing Oozie log4j edits-->
            <AppenderRef ref="stdout"/>
            <AppenderRef ref="stderr"/>
        </Root>
        <Logger name="org.apache.kafka" level="warn" />
    </Loggers>
</Configuration>

当我使用Spring Boot和Spring Cloud开发微服务时,我想从配置服务器中读取kafka连接的详细信息。我该怎么办?

1 个答案:

答案 0 :(得分:1)

据我所知,您可以在Log4J配置中使用系统属性:${sys:some.property}https://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution

我认为在引导上下文中,从配置服务器读取属性后,您可以将Kafka连接的属性公开到系统属性中,并且可用于下游日志记录配置:https://github.com/spring-cloud/spring-cloud-commons/blob/master/docs/src/main/asciidoc/spring-cloud-commons.adoc#the-bootstrap-application-context