将Jersey JUL重定向到Log4j2 [英] Redirect Jersey JUL logging to Log4j2

查看:230
本文介绍了将Jersey JUL重定向到Log4j2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将Jersey请求/响应日志重定向到我的log4j2。

I need to redirect Jersey request/response log to my log4j2.

我在上使用此代码启用了Jersey日志记录ApplicationJAXRS扩展申请

@Override
public Set<Class<?>> getClasses() {
    return new HashSet<Class<?>>() {{
        add(LoggingFilter.class);
    }};
    }

看来Jersey在内部使用JUL(Java Logging),默认输出是STDOUT。此时我可以在Eclipse控制台上看到STDOUT。

It seems that Jersey uses JUL (Java Logging) internally and the default output is STDOUT. At this moment I can see the STDOUT on Eclipse Console.

Log4j2文档中有一节关于 JDK日志记录适配器
它说

The Log4j2 documentation have a section about JDK Logging Adapter. It says


要使用JDK日志记录适配器,必须设置系统属性 java.util.logging。经理 org.apache.logging.log4j.jul.LogManager

这必须通过命令行完成(即,在对LogManager或Logger进行任何调用之前,使用-Djava.util.logging.manager = org.apache.logging.log4j.jul.LogManager参数)或使用 System.setProperty()

This must be done either through the command line (i.e., using the -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager argument) or by using System.setProperty() before any calls are made to LogManager or Logger.

在任何Logger调用之前调用 System.setProperty(*)我试图在我的 Aplication 类中将它放在 @PostConstruct 上。

To call the System.setProperty(*) before any Logger call I've tried to put it on a @PostConstruct in my Aplication class.

@PostConstruct
    public void init() {
        System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
    }

我可以让它登录我的日志文件。

I could get it to log on my logging files.

这是我的log4j2.xml:

This is my log4j2.xml:

    <Appenders>
        <RollingFile name="RollingFile" fileName="${log-path}/${name}.log" 
            filePattern="${log-path}/${date:yyyy-MM}/${name}-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout>
                <Pattern>%d{dd-MM-yy HH:mm:ss,SSS} %-5p [%t] (%F:%L) - %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
            <DefaultRolloverStrategy max="10" />
        </RollingFile>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console" level="debug"/>
            <AppenderRef ref="RollingFile" level="debug"/>
        </Root>
    </Loggers>
</Configuration>


推荐答案

我想你必须设置启动时的系统属性。将代码添加到init方法以查看是否设置了系统属性。

I would imagine that you have to set the system property at launch. Add code to your init method to see if setting the system property worked.

@PostConstruct
public void init() {
    String cn = "org.apache.logging.log4j.jul.LogManager";
    System.setProperty("java.util.logging.manager", cn);
    LogManager lm = LogManager.getLogManager();
    if (!cn.equals(lm.getClass().getName())) {
       try {
           ClassLoader.getSystemClassLoader().loadClass(cn);
       } catch (ClassNotFoundException cnfe) {
          throw new IllegalStateException("Jars not in system class path.", cnfe);
       }
       throw new IllegalStateException("Found " + lm.getClass().getName() + " set as launch param instead.");
    }
}

这篇关于将Jersey JUL重定向到Log4j2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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