sasikals26
sasikals26

Reputation: 865

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,

Upvotes: 2

Views: 7706

Answers (3)

A_Di-Matteo
A_Di-Matteo

Reputation: 27862

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.

Upvotes: 5

sasikals26
sasikals26

Reputation: 865

Yes here is the solution, We can do it by two ways as below,

  1. By configuring the below system variable in classpath

    -Dorg.apache.cxf.Logger=org.apache.cxf.common.logging.Log4jLogger

For reference- This is how i configured in eclipse

enter image description here

  1. By placing the org.apache.cxf.Logger file under WEB-INF/classes/META-INF/cxf/

Upvotes: 0

dur
dur

Reputation: 17010

As you can read on Apache CXF - Debbuging and Logging CXF uses Java SE Logging, but you can change the logging implementation:

As noted above, CXF uses the java.util.logging package ("Java SE Logging") by default. But it is possible to switch CXF to instead use Log4J. This is achieved through the use of configuration files. There are two options to bootstrapping CXF logging and each is listed below:

  • Add the following system property to the classpath from which CXF is initialized:

    -Dorg.apache.cxf.Logger=org.apache.cxf.common.logging.Log4jLogger

  • Add the file META-INF/cxf/org.apache.cxf.Logger to the classpath and make sure it contains the following content:

    org.apache.cxf.common.logging.Log4jLogger

You have to change the contents of your META-INF/cxf/org.apache.cxf.Logger file from org.apache.cxf.Logger to org.apache.cxf.common.logging.Log4jLogger

Upvotes: 3

Related Questions