Configure Log4j with CXF

Configure Log4j with CXF explains step by step details of configuring Log4j and Apache CXF Webservice.

For every application logging has a vital role because it is a must for various debugging purpose, CXF framework have in-built feature for enabling logging of request and response payloads

log4j is commonly used logging framework in java, this tutorial is using log4j as the logging tool.

You can also see server side/client side logging Apache CXF Logging

Here I am showing How to use Log4j for logging CXF soap messages

I am going to reuse my CXF Web Service Tutorial

You need to download following additional library

  1. log4j

Please see the project structure on below screenshot

Log4j with CXF

Now you need to create two additional files, 1) org.apache.cxf.Logger 2) log4j.properties, you can see the file name and its contents below, please drop both of these files according to the above screen shot.

org.apache.cxf.Logger

org.apache.cxf.common.logging.Log4jLogger

log4j.properties

# Root logger option
log4j.rootLogger=INFO, file, stdout
 
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\wsimport\\log.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

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

cxf.xml

Modify cxf.xml (add inInterceptors & outInterceptors) in order to show SOAP inbound/outbound messages

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:cxf="http://cxf.apache.org/core" 
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

   <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" id="loggingInInterceptor" />
   <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" id="logOutInterceptor" />

	<cxf:bus>
		<cxf:ininterceptors>
			<ref bean="loggingInInterceptor" />
		</cxf:ininterceptors>
		<cxf:outinterceptors>
			<ref bean="logOutInterceptor" />
		</cxf:outinterceptors>
	</cxf:bus>

    <jaxws:endpoint address="/ChangeStudent" id="changeStudent" implementor="com.student.ChangeStudentDetailsImpl" />

</beans>

you can also see CXF client example in order to run this service

After running the client, you can see the logs on console as well as configured directory, in our case it is C:\\wsimport\\log.txt

log.txt

02:14:13,781  INFO XmlWebApplicationContext:456 - Refreshing Root WebApplicationContext: startup date [Sat Mar 09 02:14:13 IST 2013]; root of context hierarchy
02:14:13,953  INFO XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [META-INF/cxf/cxf.xml]
02:14:14,203  INFO XmlBeanDefinitionReader:315 - Loading XML bean definitions from URL [jndi:/localhost/CXFTutorial/WEB-INF/cxf-servlet.xml]
02:14:14,765  INFO DefaultListableBeanFactory:557 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFact[email protected]: defining beans [cxf,org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor,org.apache.cxf.bus.spring.Jsr250BeanPostProcessor,org.apache.cxf.bus.spring.BusExtensionPostProcessor,loggingInInterceptor,logOutInterceptor,cxf.config0,changeStudent]; root of factory hierarchy
02:14:16,296  INFO ReflectionServiceFactoryBean:442 - Creating Service {http://student.com/}ChangeStudentDetailsImplService from class com.student.ChangeStudentDetails
02:14:18,906  INFO ServerImpl:94 - Setting the server's publish address to be /ChangeStudent
02:14:55,296  INFO ReflectionServiceFactoryBean:442 - Creating Service {http://student.com/}ChangeStudentDetailsService from class com.student.ChangeStudentDetails
02:14:58,750  INFO ChangeStudentDetails:234 - Outbound Message
---------------------------
ID: 1
Address: http://localhost:8080/CXFTutorial/services/ChangeStudent?wsdl
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: Rockey
--------------------------------------
02:14:59,468  INFO ChangeStudentDetails:234 - Inbound Message
----------------------------
ID: 1
Address: http://localhost:8080/CXFTutorial/services/ChangeStudent?wsdl
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[*/*], cache-control=[no-cache], connection=[keep-alive], Content-Length=[206], content-type=[text/xml; charset=UTF-8], host=[localhost:8080], pragma=[no-cache], SOAPAction=[""], user-agent=[Apache CXF 2.7.2]}
Payload: Rockey
--------------------------------------
02:15:00,171  INFO ChangeStudentDetails:234 - Outbound Message
---------------------------
ID: 1
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: Rockey
--------------------------------------
02:15:00,203  INFO ChangeStudentDetails:234 - Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {content-type=[text/xml;charset=UTF-8], Date=[Fri, 08 Mar 2013 20:45:00 GMT], Server=[Apache-Coyote/1.1], transfer-encoding=[chunked]}
Payload: Rockey
--------------------------------------

 






2 Responses to "Configure Log4j with CXF"
  1. alkis 2012-07-12 07:55:48.0
  1. admin 2012-07-13 07:55:48.0

Your email address will not be published. Required fields are marked *