不断获取“以下一组记录器可能已被访问”的logback

时间:2016-05-04 05:14:35

标签: java logging intellij-idea logback slf4j

我正在使用logback登录我的应用程序,并且每次运行应用程序时都会不断收到以下警告。当我进行单元测试时,每个班级都会打印出来!

SLF4J: The following set of substitute loggers may have been accessed
SLF4J: during the initialization phase. Logging calls during this
SLF4J: phase were not honored. However, subsequent logging calls to these
SLF4J: loggers will work as normally expected.
SLF4J: See also http://www.slf4j.org/codes.html#substituteLogger
SLF4J: com.kohls.kube.service.sdk.logging.CustomAppender
SLF4J: com.kohls.kube.service.sdk.logging.appender.mqtt.messaging.ClientMqttImpl
SLF4J: com.kohls.service.sdk.messaging.TCPMessager
SLF4J: com.kohls.service.sdk.logging.appender.mqtt.MQTTAppender
SLF4J: com.kohls.service.sdk.messaging.EdgeNodeMessager
SLF4J: com.kohls.service.sdk.messaging.SubscriptionTask

我已将logback配置为在某个位置查找配置文件,下面是日志工厂类的外观。

public class LoggerFactory {

static {
    System.setProperty("logback.configurationFile", System.getProperty("user.dir") + "\\" + "logbackConfig.xml");
}

private LoggerFactory() {

}

public static Logger getLogger(Class className) {
    return org.slf4j.LoggerFactory.getLogger(className);
}

}

然后我正在检索记录器,如

private static Logger logger = LoggerFactory.getLogger(ClassName);

我的配置XML文件如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <statusListener class="ch.qos.logback.core.status.NopStatusListener"/>

    <!-- Send debug messages to System.out -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <appender name="MQTT" class="com.kohls.service.sdk.logging.appender.mqtt.MQTTAppender">
        <!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
        <url>tcp://localhost:1883</url>
        <clientId>MqttAppender</clientId>
        <topic>log</topic>
    </appender>

    <!-- Send debug messages to a file at "c:/jcg.log" -->
    <appender name="FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>./UpdaterLog.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0} - %msg%n</Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>./archive.%i.log.zip</FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>10</MaxIndex>
        </rollingPolicy>

        <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>2MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <!-- By default, the level of the root level is set to DEBUG -->
    <root level="DEBUG">
        <!--<appender-ref ref="MQTT"/>-->
        <!--<appender-ref ref="FILE"/>-->
        <appender-ref ref="STDOUT"/>
    </root>

</configuration>

我确保在任何地方都没有其他XML。只有这一个。我在这做错了什么?为什么我不断收到这个警告?请指教。

1 个答案:

答案 0 :(得分:4)

你没有做错任何事。您的应用程序在进行第一次LoggerFactory.getLogger调用时已经是多线程的。早期版本的SLF4J(1.7.14及更早版本)会发出上述警告消息。更新版本的SLF4J(1.7.15及更高版本)将捕获在SLF4J初始化阶段发出的日志消息,并将在之后重播它们。

更新到更新版本的SLF4J可能会解决问题。或者,您可以在应用程序启动时更早地初始化SLF4J。