泽西服务器日志记录/w Logback [英] Jersey Server Logging /w Logback

查看:234
本文介绍了泽西服务器日志记录/w Logback的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何才能使Jersey LoggingFilter与logback一起使用?我看到了这篇文章:

How would I go about getting Jersey LoggingFilter working with logback? I saw this post:

如何在服务器上获取球衣日志?

,但不幸的是,它依赖于java.util.Logger.对于web.xml配置,我不是很精通,所以我不知道如何提供init-param不同的记录器.

but it unfortunately relies on the java.util.Logger. I am not super well versed when it comes to web.xml configuration so I would not know how to go about offering the init-param different loggers.

请注意,我正在使用Spring 3进行依赖项注入,但是正在使用代码为每个类创建记录器:

Note that I am using Spring 3 for dependency injection, but am creating loggers per class with the code:

Logger logger = LoggerFactory.getLogger(MyClass.class);

希望这是足够的信息.如果没有,让我知道.

Hope this is enough information. If not, let me know.

我的Web容器是Tomcat 7.0.12.

My web container is Tomcat 7.0.12.

推荐答案

我试图将Jersey-Spring4与logback一起使用,但无法通过LoggingFeature将访问日志重定向到我的logback-logger. logback-logger的配置类似于<logger name="org.glassfish.jersey" level="INFO"/>,我认为这应该可以解决问题.

I tried to use Jersey-Spring4 with logback and was not able to redirect access logs via the LoggingFeature to my logback-logger. The logback-logger was configured like <logger name="org.glassfish.jersey" level="INFO"/> and I assumed this should do the trick.

在得知Jersey正在使用JUL之后,我感到难过了几分钟,然后继续使用org.slf4j:jul-to-slf4j. 要使其正常工作,我必须

After I learned that Jersey is using JUL, I was sad for a few minutes and carried on with using org.slf4j:jul-to-slf4j. To make it work I had to

private void registerLogback(final ServletContext servletContext) {
    servletContext.addListener(new LogbackConfigListener());

    //this is only necessary if you externalise logback.xml from resources     
    try { LogbackConfigurer.initLogging(servletContext.getRealPath("/WEB-INF/logback.xml")); }
    catch (FileNotFoundException | JoranException e) { e.printStackTrace(); }

    LogManager.getLogManager().reset();
    SLF4JBridgeHandler.removeHandlersForRootLogger();
    SLF4JBridgeHandler.install();
}

在我的WebApplicationInitializer.onStartup(ServletContext servletContext)中.

我的ResourceConfig看起来像

register(new LoggingFeature(createLogger(), Level.ALL, LoggingFeature.DEFAULT_VERBOSITY, 0)); 

private Logger createLogger() {
    final ConsoleHandler consoleHandler = new ConsoleHandler();
    consoleHandler.setLevel(Level.ALL);

    final Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
    logger.setLevel(Level.ALL);
    logger.addHandler(consoleHandler);

    return logger;
}

记录到控制台的访问权限.可悲的是,删除ConsoleHandler并没有导致日志被记录到logback-logger中,而这只是被吞噬了. 但是控制台上的 red 日志语句向我暗示了这可能是STDERR,尽管它们是INFO.

What logged access to the console. Removing the ConsoleHandler sadly did not end up in logs being logged to the logback-logger, the just got swallowed. But the red log statements on the console gave me the hint that this could be STDERR, albeit their were INFO.

最后> LoggingFeature无法正确记录关于构造函数的配置给了我缺失的部分.

Finally LoggingFeature not logging correctly based on configuration on constructor gave me the missing piece.

我的最终作品ResourceConfig

register(new LoggingFeature(Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME), Level.INFO, LoggingFeature.DEFAULT_VERBOSITY, 0));

LoggingFeature.DEFAULT_LOGGER_NAMELevel.INFO似乎是关键部分.

最后我不知道到底是哪里出了问题,但这是我的猜测的结果,并且有效.如果这可以帮助任何人,我都会感到高兴.

In the end I don't know exactly went wrong, but this is the result of my guessing and it works. If this helps anybody I am happy.

这篇关于泽西服务器日志记录/w Logback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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