【问题标题】:Apache CXF and Log4j2Apache CXF 和 Log4j2
【发布时间】:2019-07-12 15:50:06
【问题描述】:

我在独立的 Java 应用程序中运行嵌入式 JAX-RS Web 服务。我使用 Apache CXF 3.3.0 和 jetty 作为 Web 容器。我还使用 Spring 来配置 Jetty 和服务 bean。

我的应用程序使用 log4j2,我正在尝试记录 CXF 容器收到的 URL 以进行故障排除。

我在WebService接口中添加了如下注解:

@InInterceptors(interceptors = "org.apache.cxf.interceptor.LoggingInInterceptor")

由于我使用 Jetty 而不是 servlet 容器,因此此处记录的说明不起作用: Apache CXF - General CXF Logging

当我在 Eclipse 中运行应用程序时,URL 将以红色打印到控制台。我用红色来记录标准错误。

但是在使用 bash 重定向这些描述符时,输出不存在于 log4j 日志文件中,也不会将输出发送到 stderr 或 stdout:

1> ${LOG_DIR}/stdout.log \
2> ${LOG_DIR}/stderr.log &

【问题讨论】:

    标签: java cxf log4j2


    【解决方案1】:

    我无法通过使用带有 log4j2 的 org.apache.cxf.interceptor.LoggingInInterceptor 来记录 REQUEST_URL

    通过创建自己的InInterceptor 类,我能够非常轻松地解决这个问题。

    以下类将传入的REQUEST_URL 很好地记录到我的 log4j2 附加程序中:

    public class RefDataInInterceptor extends AbstractPhaseInterceptor<Message> {
       private static final Logger log = 
          LogManager.getLogger(RefDataInInterceptor.class);
    
       public RefDataInInterceptor() {
          super(Phase.RECEIVE);
       }
    
       @Override
       public void handleMessage(Message msg) throws Fault {
          log.info(msg.getContextualProperty(Message.REQUEST_URL));
       }
    }
    

    我所要做的就是替换:

    @InInterceptors(interceptors = "org.apache.cxf.interceptor.LoggingInInterceptor")
    

    与:

    @InInterceptors(interceptors = "com.aqua.refdata.RefDataInInterceptor")
    

    这是输出到控制台和日志文件的 log4j2:

    2019-07-12 16:13:54.811 [3710][qtp172794870-21][INFO http://localhost:9000/refdata/exec/getDBParam
    eters [RefDataInInterceptor.java:19]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      相关资源
      最近更新 更多