sasikals26 sasikals26 - 5 months ago 101
Java Question

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,

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.common.logging.Log4jLogger


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

enter image description here

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,

Answer

Make sure you put in your org.apache.cxf.Logger file the one line text

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.

Finally, looking at your log4j configuration file, you missed to set the CXF logger level at INFO as below:

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

Try to add this line at the end of your log4j configuration.