简体   繁体   English

使用CXF中的log4j登录和注销Interceptor消息

[英]Logging In and out Interceptor messages with log4j in CXF

I am trying to redirect LoggingInInterceptor and LoggingOutInterceptor messages of my rest webservices from console to file using log4j as below, 我正在尝试使用log4j将我的其余web服务的LoggingInInterceptor和LoggingOutInterceptor消息从控制台重定向到文件,如下所示,

cxf.xml cxf.xml

        <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xmlns:cxf="http://cxf.apache.org/core"
    xsi:schemaLocation="
    http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://cxf.apache.org/jaxrs
    http://cxf.apache.org/schemas/jaxrs.xsd">

        <jaxrs:server id="base" address="/Restful">

            <jaxrs:serviceBeans>
                <ref bean="Service" />
            </jaxrs:serviceBeans>

            <jaxrs:features>
                <cxf:logging />
            </jaxrs:features>

        </jaxrs:server>

    <bean id="Service" class="com.xxx.yyy.services.ServiceImpl" />

    </beans>

org.apache.cxf.Logger file org.apache.cxf.Logger文件

org.apache.cxf.common.logging.Log4jLogger

log4j.properties log4j.properties

        # Root logger option
    log4j.rootLogger=INFO, file, stdout

    ## more informations
    # http://cxf.apache.org/docs/debugging-and-logging.html
    # Direct log messages to a log file
    log4j.appender.file=org.apache.log4j.RollingFileAppender
    log4j.appender.file.File=D:\\ServiceLog.txt
    log4j.appender.file.MaxFileSize=1MB
    log4j.appender.file.MaxBackupIndex=1
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

Addtional details, 附加细节,

CXF 3.1.4 and Java 7 CXF 3.1.4和Java 7

在此输入图像描述

Here i am receiving both the messages in console but it is not writing in the file. 在这里,我在控制台中接收这两个消息,但它没有在文件中写入。

It would be appreciated if anyone could help me here.. 如果有人能在这里帮助我,我们将不胜感激。

Thanks, 谢谢,

Make sure you put in your org.apache.cxf.Logger file the one line text 确保在org.apache.cxf.Logger文件中输入一行文本

org.apache.cxf.common.logging.Log4jLogger

Also, make sure it is part of the WAR classpath, hence it should end up under WEB-INF/classes/META-INF/cxf/org.apache.cxf.Logger once your WAR is properly packaged. 另外,确保它是WAR类路径的一部分,因此一旦你的WAR正确打包,它应该在WEB-INF/classes/META-INF/cxf/org.apache.cxf.Logger下结束。

Finally, looking at your log4j configuration file, you missed to set the CXF logger level at INFO as below: 最后,查看log4j配置文件,您错过了将INFF记录器级别设置为INFO ,如下所示:

log4j.appender.org.apache.cxf.Threshold=INFO

Try to add this line at the end of your log4j configuration. 尝试在log4j配置的末尾添加此行。

As you can read on Apache CXF - Debbuging and Logging CXF uses Java SE Logging, but you can change the logging implementation: 正如您可以阅读Apache CXF - Debbuging和Logging CXF使用Java SE Logging,但您可以更改日志记录实现:

As noted above, CXF uses the java.util.logging package ("Java SE Logging") by default. 如上所述,默认情况下,CXF使用java.util.logging包(“Java SE Logging”)。 But it is possible to switch CXF to instead use Log4J. 但是可以将CXF切换为使用Log4J。 This is achieved through the use of configuration files. 这是通过使用配置文件实现的。 There are two options to bootstrapping CXF logging and each is listed below: 引导CXF日志记录有两个选项,每个选项列在下面:

  • Add the following system property to the classpath from which CXF is initialized: 将以下系统属性添加到初始化CXF的类路径中:

    -Dorg.apache.cxf.Logger=org.apache.cxf.common.logging.Log4jLogger

  • Add the file META-INF/cxf/org.apache.cxf.Logger to the classpath and make sure it contains the following content: 将文件META-INF/cxf/org.apache.cxf.Logger到类路径中,并确保它包含以下内容:

    org.apache.cxf.common.logging.Log4jLogger

You have to change the contents of your META-INF/cxf/org.apache.cxf.Logger file from org.apache.cxf.Logger to org.apache.cxf.common.logging.Log4jLogger 您必须将META-INF/cxf/org.apache.cxf.Logger文件的内容从org.apache.cxf.Logger更改为org.apache.cxf.common.logging.Log4jLogger

Yes here is the solution, We can do it by two ways as below, 是的,这是解决方案,我们可以通过以下两种方式实现,

  1. By configuring the below system variable in classpath 通过在类路径中配置以下系统变量

    -Dorg.apache.cxf.Logger=org.apache.cxf.common.logging.Log4jLogger -Dorg.apache.cxf.Logger = org.apache.cxf.common.logging.Log4jLogger

For reference- This is how i configured in eclipse 供参考 - 这是我在eclipse中配置的方式

在此输入图像描述

  1. By placing the org.apache.cxf.Logger file under WEB-INF/classes/META-INF/cxf/ org.apache.cxf.Logger文件放在WEB-INF / classes / META-INF / cxf /下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM