How to log CXF webservice requests with log4j2?

2019-02-26 06:27发布

问题:

I'd like to log all incoming and outgoing CXF requests to a specific logfile. But all I get with the following configuration is a console output. What is wrong here?

log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="CONSOLE" target="SYSTEM_OUT">
            <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
        </Console>

        <RollingFile name="CXF" fileName="cxf.log">
            <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <logger name="org.apache.cxf" additivity="false" level="info">
                <AppenderRef ref="CXF"/>
        </logger>
        <logger name="org.apache.cxf.interceptor.LoggingInInterceptor" additivity="false" level="info">
            <AppenderRef ref="CXF" />
        </logger>
        <logger name="org.apache.cxf.interceptor.LoggingOutInterceptor" additivity="false" level="info">
            <AppenderRef ref="CXF" />
        </logger>

        <Root level="all">
            <AppenderRef ref="CONSOLE" />
        </Root>
    </Loggers>
</Configuration>

src/main/resources/META-INF/cxf/org.apache.cxf.Logger:

org.apache.cxf.common.logging.Log4jLogger

回答1:

As a workaround, I'm now using org.apache.cxf.common.logging.Slf4jLogger and the bridge dependency.

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.0</version>
    </dependency>


回答2:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">

<Properties>
    <property name="multiplan">logs/multiplan.log</property>        
    <property name="webservice">logs/webservice.log</property>
    <property name="sql">logs/sql.log</property>        
    <property name="mongoexceptions">logs/mongoexceptions.log</property>

</Properties>

<Appenders>
<RollingFile name="mongoexceptions-all" fileName="${mongoexceptions}"
    filePattern="${mongoexceptions}.%i" bufferedIO="false" bufferSize="8192">
        <PatternLayout>
            <pattern>%d{dd MMM HH:mm:ss:SSS} %p [%t] - %m ---- %c%n</pattern>
        </PatternLayout>
        <Policies>
            <SizeBasedTriggeringPolicy size="10000KB" />
        </Policies>
        <DefaultRolloverStrategy max="10"/>
    </RollingFile>

    <RollingFile name="multiplan-all" fileName="${multiplan}"
    filePattern="${multiplan}.%i" bufferedIO="false" bufferSize="8192">
        <PatternLayout>
            <pattern>%d{dd MMM HH:mm:ss:SSS} %p [%t] - %m ---- %c%n</pattern>
        </PatternLayout>
        <Policies>
            <SizeBasedTriggeringPolicy size="10000KB" />
        </Policies>
        <DefaultRolloverStrategy max="20"/>
    </RollingFile>      

    <RollingFile name="webservice-all" fileName="${webservice}" filePattern="${webservice}.%i" bufferedIO="false" bufferSize="8192">
       <PatternLayout>
            <pattern>%d{dd MMM HH:mm:ss:SSS} %p [%t] - %m %n</pattern>
        </PatternLayout>
        <Policies>
            <SizeBasedTriggeringPolicy size="10000KB" />
        </Policies>
        <DefaultRolloverStrategy max="5"/>

    </RollingFile>

     <RollingFile name="sql-all" fileName="${sql}" filePattern="${sql}.%i" bufferedIO="true" bufferSize="8192">
        <PatternLayout pattern="%d [%t] %-5p %c - %m%n" />
        <Policies>
            <SizeBasedTriggeringPolicy size="1000KB" />
        </Policies>
        <DefaultRolloverStrategy max="1"/>
    </RollingFile>        
</Appenders>
<Loggers>

    <Root level="info" additivity="false">
        <AppenderRef ref="multiplan-all"/>
    </Root>
    <Logger name="org.apache.cxf" additivity="false" level="warn">
            <appender-ref ref="multiplan-all"/>
    </Logger>
    <Logger name="org.springframework" additivity="false" level="warn">
            <appender-ref ref="multiplan-all"/>
    </Logger>
    <Logger name="org.eclipse.jetty" additivity="false" level="warn">
            <appender-ref ref="multiplan-all"/>
    </Logger>
    <Logger name="com.multiplan.bam.db.edp.EventAuditDAO"  level="error" additivity="false">
        <appender-ref ref="mongoexceptions-all" />
    </Logger>
    <Logger name="com.multiplan.bam.db.edp.ExceptionsLogDAO"  level="error" additivity="false">
        <appender-ref ref="mongoexceptions-all" />
    </Logger>
    <Logger name="com.multiplan.bam.db.edp.ClaimDAO"  level="debug">
        <appender-ref ref="webservice-all" />
    </Logger>
    <Logger name="com.multiplan.bam.db.edp.CPDSummaryDAO"  level="debug">
        <appender-ref ref="webservice-all" />
    </Logger>
    <Logger name="DBConnectionFactory" level="info">
        <appender-ref ref="sql-all"/>
    </Logger>       
</Loggers>
</Configuration>

Here is the working example.