spring boot logging问题

时间:2016-08-10 19:51:20

标签: spring-boot log4j logback

我是SpringBoot Project的新手,我正在尝试配置logback-spring.xml

在我的项目pom.xml文件中记录依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j</artifactId>
</dependency>

application.proerties中记录配置:

logging.config=classpath:logback-spring.xml
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR

当我运行可执行文件时,它会抛出异常:

  SLF4J: Class path contains multiple SLF4J bindings.
    SLF4J: Found binding in [jar:file:/target/rentacoder.war!/WEB-INF/lib/slf4j-log4j12-1.7.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]
    SLF4J: Found binding in [jar:file:/target/rentacoder.war!/WEB-INF/lib/slf4j-simple-1.7.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]
    SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
    SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
    log4j:WARN Continuable parsing error 2 and column 16
    log4j:WARN Document root element "configuration", must match DOCTYPE root "null".
    log4j:WARN Continuable parsing error 2 and column 16
    log4j:WARN Document is invalid: no grammar found.
    log4j:WARN The <configuration> element has been deprecated.
    log4j:WARN Use the <log4j:configuration> element instead.
    log4j:WARN Unrecognized element include
    log4j:ERROR Could not create an Appender. Reported error follows.
    java.lang.ClassNotFoundException: ch.qos.logback.core.FileAppender
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at org.springframework.boot.loader.LaunchedURLClassLoader.doLoadClass(LaunchedURLClassLoader.java:178)
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:142)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)

我的logback-spring.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="org.springframework.web" level="DEBUG"/>

    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>myApp.log</file>
        <encoder>
            <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="org.apache.velocity" level="OFF" />

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

有人可以对此有所了解吗?我该如何解决?

1 个答案:

答案 0 :(得分:1)

有几个问题。

首先,要使用logback-spring.xml配置您的日志记录,您需要使用Logback,但您已排除使用Logback的spring-boot-starter-logging,而使用的是使用Log4j的spring-boot-starter-log4j。如果你想使用logback-spring.xml,你应该从你的pom中删除排除。

其次,SLF4J: Found binding in [jar:file:/target/rentacoder.war!/WEB-INF/lib/slf4j-simple-1.7.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]表示您在类路径上也有slf4j-simple。鉴于您提供的信息,我无法告诉它来自哪里,但您需要将其从依赖项中删除。您可以使用mvn dependency:tree来帮助您确定其来源,并在适当时删除/排除。