简体   繁体   中英

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

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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