泽西LoggingFilter与log4j [英] Jersey LoggingFilter with log4j

查看:129
本文介绍了泽西LoggingFilter与log4j的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用泽西开发的休息服务,我有一个ContainerRequestFilters用于打印请求,如下所示:

I have a rest service developed with jersey, I have a ContainerRequestFilters for print the request, as follows:

<init-param>
    <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
    <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
</init-param>

我使用log4j记录了post方法。但LoggingFilter在日志中打印的log4j不同。 LogginFilter有没有办法使用log4j的配置?

and I have logger in the post methods using log4j. But the LoggingFilter print in the log different log4j. Is there any way that LogginFilter use the log4j's configuration?

我在log4j.xml文件中试过这个:

I tried this in the log4j.xml file:

<logger name="com.sun.jersey.api.container.filter.LoggingFilter">
    <level value="info" />
    <appender-ref ref="ROOT" />
    <appender-ref ref="CONSOLE" />
</logger>

但不起作用:(

推荐答案

Jersey确实使用了jdk记录器。你可以使用 SLF4JBridgeHandler 将JDK日志消息传输到您的日志记录基础结构。(参见 Claus Nielsen的Blogpost

Jersey uses indeed the jdk logger. You can use the SLF4JBridgeHandler to transfer the JDK log messages to your logging infrastructure. (see Claus Nielsen's Blogpost)

SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();

所以为了在Spring应用程序中获取它,我使用 @PostConstruct 注释。

So to get this picked up in your Spring application, I used the @PostConstruct annotation.

@Component
public class JerseyLoggingBridge {    
    @PostConstruct
    private void init() {
        SLF4JBridgeHandler.removeHandlersForRootLogger();
        SLF4JBridgeHandler.install();
    }    
}

这篇关于泽西LoggingFilter与log4j的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆