How to load log4j2.xml via Spring to log CXF inbound outbound request/response xmls

喜你入骨 提交于 2019-12-24 22:09:06

问题


I have log4j.xml in my war which I used to log incoming and outgoing request and responses to a specific log file.

This is my log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>

<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n" />
</layout>
</appender>

<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true" />
<param name="file" value="Folder/ABC.log" />
<param name="MaxFileSize" value="50MB" />
<param name="MaxBackupIndex" value="50" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n" />
</layout>
</appender>
<logger name="org.apache.cxf">
<level value="INFO" />
<appender-ref ref="fileAppender" />
</logger>
<root>
<priority value="DEBUG" />
<appender-ref ref="consoleAppender" />
<appender-ref ref="fileAppender" />
</root>

</log4j:configuration>

Added log4j configuration in applicationContext.xml

<!--This is for log4j configuration -->
<bean id="log4jInitialization"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
<property name="targetMethod" value="initLogging" />
<property name="arguments">
<list>
<value>classpath:/log4j.xml</value>
</list>
</property>
</bean>

Would like to do similar configuration for log4j2. It would be very helpful, if can someone share the log4j2 configuration. I searched a lot in google, but nothing helps.

Thank you.


回答1:


Assuming you are using CXF 2.2.8 or higher, you would need to do the following:

step 1) Create a file 'META-INF/cxf/org.apache.cxf.Logger' on the classpath containing the following: org.apache.cxf.common.logging.Slf4jLogger

step 2) If you want to log all messages, create a CXF LoggingFeature, set the prettyLogging property to true and add it to the CXF bus.

step 3) Add the needed jar files for Log4j2 and the Log4j 2 SLF4J Binding. If you are using maven, include following dependencies:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>${log4j2.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>${log4j2.version}</version>
</dependency>

I've created a blog post which explains how to configure CXF for log4j2 in more detail.



来源:https://stackoverflow.com/questions/26364822/how-to-load-log4j2-xml-via-spring-to-log-cxf-inbound-outbound-request-response-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!