Logging Apache CXF dengan Log4j

Apache CXF memiliki fitur pencatatan dari setiap request dan response yang dibentuk. Pencatatan tersebut disimpan dalam sebuah log yang sangat penting untuk digunakan nantinya.

log4j merupakan framework longging yang ada di java dan dapat digunakan dalam Apache CXF. Sebelumnya pastikan kalian telah memiliki project JaxWSHelloWorld di tulisan saya sebelumnya.

Yang dibutuhkan:
- log4j

Letakkan jar log4j di dalam folder /WebContent/WEB-INF/lib

Pada folder Project JaxWSHelloWorld di dalam eclipse buatlah folder META-INF yang berisi folder cxf. Kemudian buat file bernama org.apache.cxf.Logger didalamnya yang berisi:
org.apache.cxf.common.logging.Log4jLogger

Setelah itu buat file bernama log4j.properties yang berisi:
# Root logger option
log4j.rootLogger=INFO, file, stout
 
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/Users/rizupz/WSLog/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 stout
log4j.appender.sdout=org.apache.log4j.ConsoleAppender
log4j.appender.sdout.Target=System.out
log4j.appender.sdout.layout=org.apache.log4j.PatternLayout
log4j.appender.sdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

Sehingga susunan project akan menjadi


Klik dan ubah file cxf.xml yang berada dibawah folder /WebContent/WEB-INF menjadi
<?xml version="1.0" encoding="UTF-8">
<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-2.5.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 xmlns:tns="http://ws.rizupz.com/" id="helloservice"
  implementor="com.rizupz.ws.HelloServiceImpl" wsdlLocation="wsdl/helloserviceimpl.wsdl"
  endpointName="tns:HelloServiceImplPort" serviceName="tns:HelloServiceImplService"
  address="/HelloServiceImplPort">  
 </jaxws:endpoint>
</beans>

Simpan dan jalankan webservice, lalu lihat hasil log file yang terbentuk dan tersimpan di /Users/rizupz/WSLog/log.txt akan berisi:

20:08:59,922  INFO ContextLoader:194 - Root WebApplicationContext: initialization started
20:08:59,993  INFO XmlWebApplicationContext:456 - Refreshing Root WebApplicationContext: startup date [Wed Nov 19 20:08:59 WIB 2014]; root of context hierarchy
20:09:00,055  INFO XmlBeanDefinitionReader:315 - Loading XML bean definitions from ServletContext resource [/WEB-INF/cxf-beans.xml]
20:09:00,615  INFO DefaultListableBeanFactory:557 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3fc49f92: defining beans [loggingInInterceptor,logOutInterceptor,org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor,cxf.config0,helloservice,cxf]; root of factory hierarchy
20:09:01,596  INFO ReflectionServiceFactoryBean:411 - Creating Service {http://ws.rizupz.com/}HelloServiceImplService from WSDL: wsdl/helloserviceimpl.wsdl
20:09:02,051  INFO ServerImpl:84 - Setting the server's publish address to be /HelloServiceImplPort
20:09:02,230  INFO ReflectionServiceFactoryBean:411 - Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}Discovery from WSDL: classpath:/org/apache/cxf/ws/discovery/wsdl/wsdd-discovery-1.1-wsdl-os.wsdl
20:09:02,295  INFO ServerImpl:84 - Setting the server's publish address to be soap.udp://239.255.255.250:3702
20:09:02,437  INFO ReflectionServiceFactoryBean:457 - Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}DiscoveryProxy from class org.apache.cxf.jaxws.support.DummyImpl
20:09:02,702  INFO DummyImpl:239 - Outbound Message
---------------------------
ID: 1
Address: soap.udp://239.255.255.250:3702
Encoding: UTF-8
Content-Type: application/soap+xml
Headers: 
Payload: http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01/Hellourn:uuid:d589b8e0-9556-4114-bc32-63ac311315c8urn:docs-oasis-open-org:ws-dd:ns:discovery:2009:01urn:uuid:a4ddcdf1-1b5c-4024-8198-e151c6e4d66eHelloServiceImpl/HelloServiceImplPort1
--------------------------------------
20:09:02,713  INFO ContextLoader:221 - Root WebApplicationContext: initialization completed in 2790 ms

Download Source Code
Logging Apache CXF dengan Log4j Logging Apache CXF dengan Log4j Reviewed by rizupz on Rabu, Agustus 20, 2014 Rating: 5

Tidak ada komentar:

Diberdayakan oleh Blogger.