log4j:WARN 找不到记录器的附加程序 (org.apache.commons.beanutils.converters.BooleanConverter)

log4j:WARN No appenders could be found for logger (org.apache.commons.beanutils.converters.BooleanConverter)

我知道这个问题经常出现,但我完全被难住了。

我正在尝试编写一个使用 Log4j2 进行日志记录的 KafkaStreams 应用程序,但我收到一个关于没有附加程序的 Log4j(版本 1)警告:

log4j:WARN No appenders could be found for logger (org.apache.commons.beanutils.converters.BooleanConverter).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

标准答案是在 pom 文件中添加一个排除项,我已经做了很多次都无济于事。

它变得更加奇怪,因为即使警告可能建议 common-beanutils,导致控制台输出错误的行是:

        final Map<String, String> serdeConfig = Collections.singletonMap(
            AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG,
            ApplicationConfiguration.SCHEMA_REGISTRY_HOSTS);

我在这上面花了一整天,但似乎一点进展都没有。

今天之后我的 pom 文件被排除项搞得一团糟,我的依赖项看起来像这样。

    <dependencies>

    <!--Configuration Dependency for importing properties file-->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-configuration2</artifactId>
        <version>2.2</version>
        <!--Excluded in Map-->
        <exclusions>
            <exclusion>
                <artifactId>commons-logging</artifactId>
                <groupId>commons-logging</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <!--Dependency used for Apache commons configuration2-->
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.9.3</version>
        <exclusions>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
            <!--Excluded in Map-->
            <exclusion>
                <artifactId>commons-logging</artifactId>
                <groupId>commons-logging</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <!--Log4j2 Dependency for logging-->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.11.0</version>
    </dependency>

    <!--Required for Log4j2 Dependency for logging-->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.11.0</version>
    </dependency>

    <!--Used for Object Json Serialization and Deserialization-->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.6</version>
    </dependency>

    <!--Sumo Logic Dependency for Log4j2 logging-->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
        <exclusions>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
            <!--Excluded in Map-->
            <exclusion>
                <artifactId>commons-logging</artifactId>
                <groupId>commons-logging</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <!--Confluent Platform Dependency for streams avro serializer-->
    <dependency>
        <groupId>io.confluent</groupId>
        <artifactId>kafka-streams-avro-serde</artifactId>
        <version>3.3.0</version>
        <!--Required due to SLF4j dependency conflict-->
        <exclusions>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
            <!--Excluded in Map-->
            <exclusion>
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <!--Confluent Platform Dependency for avro serializer-->
    <dependency>
        <groupId>io.confluent</groupId>
        <artifactId>kafka-avro-serializer</artifactId>
        <version>3.3.0</version>
        <!--Excluded in Map-->
        <exclusions>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
            <exclusion>
                <artifactId>commons-logging</artifactId>
                <groupId>commons-logging</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <!--Confluent Platform Dependency for schema registry-->
    <dependency>
        <groupId>io.confluent</groupId>
        <artifactId>kafka-schema-registry-client</artifactId>
        <version>3.3.0</version>
        <!--Required due to SLF4j dependency conflict-->
        <exclusions>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!--Kafka API Client Dependency-->
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
        <version>1.1.0</version>
        <!--Excluded in Map-->
        <exclusions>
            <exclusion>
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <!--Kafka Streams API Client Dependency-->
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-streams</artifactId>
        <version>1.1.0</version>
        <!--Excluded in Map-->
        <exclusions>
            <exclusion>
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <!--Avro Serialization Dependency-->
    <dependency>
        <groupId>org.apache.avro</groupId>
        <artifactId>avro</artifactId>
        <version>1.8.2</version>
        <!--Excluded in Map-->
        <exclusions>
            <exclusion>
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <!--Avro pojo class generation Dependency-->
    <dependency>
        <groupId>org.apache.avro</groupId>
        <artifactId>avro-maven-plugin</artifactId>
        <version>1.8.2</version>
        <!--Excluded in Map-->
        <exclusions>
            <exclusion>
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <!--Avro Serialization Dependency-->
    <dependency>
        <groupId>org.apache.avro</groupId>
        <artifactId>avro-tools</artifactId>
        <version>1.8.2</version>
        <!--Excluded in Map-->
        <exclusions>
            <exclusion>
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
            <exclusion>
                <artifactId>commons-logging</artifactId>
                <groupId>commons-logging</groupId>
            </exclusion>
        </exclusions>
    </dependency>

</dependencies>

而且我已经排除了与 slf4j 相关的任何内容,甚至从我的依赖关系图中排除了此时的日志记录,目前看起来是这样的:

我的应用程序日志记录按预期工作(控制台和 RolingFileAppender),但我似乎无法消除这个顽固的小警告。

感谢大家在这里阅读我的小书;特别是如果您有任何建议!

最终我选择了上面评论中的选项 2。

这样做的原因是,与源代码控制中的附加文件相比,在生产中保持日志消息最少和清晰似乎更重要。此外,单独的 log4j 配置文件的第二个选项允许我轻松地从依赖项中打开额外的调试细节,如果我需要它们,比如我的 KStreams 应用程序似乎没有按预期工作,我被难住了。

具体来说,我实际上创建了两个版本的 log4j 配置,因此我对本地和(标记化的)发布版本有不同的配置,就像我的其他 configuration/properties 一样:

$ ls src/main/resources/*/*.xml
src/main/resources/local/log4j.xml*   src/main/resources/release/log4j.xml*
src/main/resources/local/log4j2.xml*  src/main/resources/release/log4j2.xml*

然后我将 log4j.xml 设置为正常但默认根记录器级别为关闭,因此它将使日志静音,除非更改并看起来像这样:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="false">

    <appender name="MyConsoleAppender" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                   value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
    </appender>

    <appender name="MyRollingFileAppender" class="org.apache.log4j.RollingFileAppender">
        <param name="append" value="true" />
        <param name="maxFileSize" value="100MB" />
        <param name="maxBackupIndex" value="10" />
        <param name="file" value="c:\Logs\KStreamsApp\Dependencies\KStreamsAppDependencies.log" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                   value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
    </appender>

    <root>
        <level value="Off" />
        <appender-ref ref="MyConsoleAppender" />
        <appender-ref ref="MyRollingFileAppender" />
    </root>

</log4j:configuration>