如何正确配置SLF4J和log4j的Spring Data Rest?

时间:2016-08-01 10:29:39

标签: spring log4j slf4j spring-data-rest

我遇到了Spring Data Rest应用程序和无法正常工作的SLF4J配置的问题。

Spring Data Rest在其原生依赖项中导入SLF4J。

我正在尝试使用log4j配置Spring Data Rest应用程序,但它不起作用

在我的pom.xml我已排除commons-logging为每个依赖项添加此排除项:

<exclusions>
    <exclusion>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
    </exclusion>
</exclusions>

这是web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>${artifactId}</display-name>

    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>data.rest.app</param-value>
    </context-param>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.properties</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/spring-web-config.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>restFilter</filter-name>
        <filter-class>mypackage.filter.RestFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>restFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>rest</servlet-name>
        <servlet-class>mypackage.loader.AppRepositoryRestDispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>rest</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

</web-app>

这是log4j.properties

log4j.rootLogger=TRACE, stdout

log4j.category.org.springframework=TRACE

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%X{principal}] %C.%M:%L (%t) - %m%n

尽管配置了Spring的“TRACE”级别, Spring Data Rest不会使用我的配置记录任何内容

这是我在应用程序开始时可以看到的日志:

INFORMATIONS: Initializing log4j from [C:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp5\wtpwebapps\data.rest.app\WEB-INF\log4j.properties]
aug 01, 2016 11:47:12 AM org.apache.catalina.core.ApplicationContext log
INFORMATIONS: Initializing Spring root WebApplicationContext
aug 01, 2016 11:47:13 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFORMATIONS: Root WebApplicationContext: initialization started

我希望initWebApplicationContext的“org.springframework.web.context.ContextLoader”方法会记录为我的Log4J配置,但以标准方式记录,忽略我的配置

相同的log4j.properties Spring MVC 配置完美配合,但不适用于Spring Data Rest。

有什么不对/遗失?

2 个答案:

答案 0 :(得分:1)

http://www.slf4j.org/legacy.html查看log4j-over-slf4j你应该只需要将依赖项添加到你的pom.xml

我使用spring-boot-sample-data-rest-archetype作为测试并修改了依赖关系,如下所示

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>jul-to-slf4j</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>log4j-over-slf4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
    </dependency>
</dependencies>

答案 1 :(得分:1)

我找到了解决方案,感谢@Essex Boy的建议。

该解决方案是Essex Boy建议和进一步排除的组合,可添加到pom.xml中。

我必须在排除后添加以下内容:

<exclusion>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jdk14</artifactId>
</exclusion>

我不得不添加此排除项,因为由于某些原因,我的项目中的依赖项使用了slf4j-jdk14。

然后,我添加了以下依赖项:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.21</version>
</dependency>

使用这两种配置,Spring Data Rest神奇地开始使用log4j和自定义配置进行记录。

那是因为开始的系统加载了org.slf4j.impl.JDK14LoggerFactory。 没有排除,在日志中我们可以看到这个警告:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp5/wtpwebapps/data.rest.app/WEB-INF/lib/slf4j-jdk14-1.7.14.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp5/wtpwebapps/data.rest.app/WEB-INF/lib/slf4j-log4j12-1.7.21.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.JDK14LoggerFactory]