簡體   English   中英

如何使用apache LoggingInterceptor,Spring和log4j隔離單個方法的日志?

[英]How can I isolate the log of a single method using apache LoggingInterceptor, Spring and log4j?

我有一個發布三種方法的Web服務,並且使用LoggingInInterceptor(擴展org.apache.cxf.interceptor.LoggingInInterceptor)和LoggingOutInterceptor(擴展org.apache.cxf.interceptor.LoggingOutInterceptor)進行日志記錄。

關鍵是我想將有關這些方法之一的信息記錄在單獨的文件中,但是我不知道是否可能。 我需要使用攔截器來執行此操作,因為請求和響應信息是自動記錄的,因此不需要編寫日志。

我已經在Internet上查找了,發現了配置日志記錄攔截器的不同方法,但與我要查找的內容無關。

我的項目使用Spring,並且已經按如下方式設置了Web服務:

log4j.properties

log4j.logger.ws.service.interceptor.LoggingInInterceptor=INFO,wsServiceLog
log4j.logger.ws.service.interceptor.LoggingOutInterceptor=INFO, wsServiceLog

log4j.appender.wsServiceLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.wsServiceLog.File=d:/logs/ws_service_methodTwo.log
log4j.appender.wsServiceLog.Append=true
log4j.appender.wsServiceLog.DatePattern='.'_yyyy-MM-dd
log4j.appender.wsServiceLog.layout=org.apache.log4j.PatternLayout
log4j.appender.wsServiceLog.layout.ConversionPattern=%-5p %d{dd MMM yyyy HH:mm:ss } %l %x: %m%n

applicationContext-ws.xml

<jaxws:endpoint id="service" implementor="#serviceImpl"
    address="/wsService" 
    serviceName="wsService" wsdlLocation="wsdl/wsService.wsdl">

    <!-- Interceptors for logging -->
    <jaxws:inInterceptors>
        <ref bean="logInboundWs"/>
    </jaxws:inInterceptors>
    <jaxws:outInterceptors>
        <ref bean="logOutboundWs"/>
    </jaxws:outInterceptors>
    <jaxws:outFaultInterceptors>
        <ref bean="logOutboundWs"/>
    </jaxws:outFaultInterceptors>
    <jaxws:inFaultInterceptors>
        <ref bean="logInboundWs"/>
    </jaxws:inFaultInterceptors>
</jaxws:endpoint>

使用此配置,所有請求和響應都記錄在文件中。 而且我不知道如何設置...

我也嘗試過在實現Web服務的類中設置攔截器,但是即使如此,我也不知道如何以單個方法為目標來重定向日志...

@WebService(endpointInterface = "ws.service.WsService")
@InInterceptors(interceptors = "ws.service.interceptor.LoggingInInterceptor")
@OutInterceptors(interceptors = "ws.service.interceptor.LoggingOutInterceptor")
public class WsServiceImpl implements WsService
{
    private static Logger LOGGER = Logger.getLogger(WsServiceImpl.class);


    public String methodOne(){
    ...
    }

    public String methodTwo(){
    ...
    }

    public String methodThree(){
    ...
    }
}

提前非常感謝您。

檢查以下示例配置:

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<!-- ============================== -->
<!-- Append SQL messages to a file. -->
<!-- ============================== -->
<appender name="SQL" class="org.apache.log4j.RollingFileAppender">
    <param name="Threshold" value="TRACE" />
    <param name="File" value="sql-statement.log" />
    <param name="Append" value="true" />
    <param name="MaxFileSize" value="5000KB" />
    <param name="MaxBackupIndex" value="100" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%m%n" />
    </layout>
</appender>



<!-- =============================== -->
<!-- Application specific categories -->
<!-- =============================== -->
<category name="your specific package" additivity="true">
    <priority value="TRACE" />
    <appender-ref ref="SQL" />
</category>

<!-- Setup the Root category -->
<root>
    <priority value="ERROR" />
</root>

基本上,您可以指定包名稱並創建其他追加程序以將其保存在其他文件中。 如果一個文件中有多個方法,則可以將它們分為兩個類,以實現不同的日志文件保存。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM