logback-对[root]没有适用的操作

时间:2018-12-06 08:09:47

标签: logback

我刚开始使用Java,并且试图通过logback配置日志记录。

日志记录不起作用,相反,我在控制台中收到一个错误:

-ERROR in ch.qos.logback.core.joran.spi.Interpreter@9:29 - no applicable action for [root], current ElementPath  is [[configuration][appender][root]]

因此找不到附加器:

-WARN in Logger[Test01] - No appenders present in context [default] for logger [Test01].

我的代码很简单:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MainClass {

    public static void main(String[] args) {
        Logger log = LoggerFactory.getLogger(MainClass.class);
        log.debug("test");    }
}

运行时完整的控制台输出如下:

08:41:43,047 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
08:41:43,047 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
08:41:43,047 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/C:/Users/klein/IdeaProjects/Maven01ArtifactId/target/classes/logback.xml]
08:41:43,196 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
08:41:43,199 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
08:41:43,209 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
08:41:43,235 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@9:29 - no applicable action for [root], current ElementPath  is [[configuration][appender][root]]
08:41:43,235 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@10:41 - no applicable action for [appender-ref], current ElementPath  is [[configuration][appender][root][appender-ref]]
08:41:43,235 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
08:41:43,235 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@78e94dcf - Registering current configuration as safe fallback point
08:41:43,235 |-WARN in Logger[Test01] - No appenders present in context [default] for logger [Test01].

我正在使用IntelliJ Community 2018.3,并与Maven一起启动了一个项目。下面是logback.xml和pom.xml。

有什么主意吗?

logback.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration debug="true">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>

        <root level="DEBUG">
            <appender-ref ref="STDOUT"/>
        </root>
    </appender>
</configuration>

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>xxx</groupId>
    <artifactId>xxx</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <slf4jVersion>1.7.25</slf4jVersion>
        <logbackVersion>1.2.3</logbackVersion>
    </properties>

<dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4jVersion}</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>${logbackVersion}</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${logbackVersion}</version>
    </dependency>
</dependencies>
</project>

1 个答案:

答案 0 :(得分:0)

在logback.xml中,将<root>部分放在<appender>部分中。 您的logback.xml应该是:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration debug="true">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>
    <root level="DEBUG">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>