Ricardo Gladwell
Ricardo Gladwell

Reputation: 3785

How do I get CXF to log exceptions to the ERROR channel?

In my current install of a Spring CXF JAX-RS Servlet, exceptions are being logged to the log4j DEBUG channel:

DEBUG http-8134-2 org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper - WebApplicationException has been caught, status: 500, message: Unrecognized field "links" (Class Result), not marked as ignorable
at [Source: {"links"}; line: 1, column: 11] (through reference chain: bbc.news.naf.elections.data.model.client.Result["links"])
javax.ws.rs.WebApplicationException: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "links" (Class Result), not marked as ignorable
at [Source: {"links":[{}; line: 1, column: 11] (through reference chain: bbc.news.naf.elections.data.model.client.Result["links"])
    at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:243)
    at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:89)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
    at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:211)
    at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:213)

Obviously, this isn't ideal. How do I get CXF to log these exception stack traces to something more relevant like WARN or ERROR?

Upvotes: 1

Views: 2676

Answers (2)

R-JANA
R-JANA

Reputation: 1138

write a simple provider class as below

public class ExceptionResponseBuilder 
  extends WebApplicationExceptionMapper {
Logger _logger = LoggerFactory.getLogger(this.getClass().getName());

public Response toResponse(WebApplicationException ex) {

//Do something to get the object you want to return. 
// ex object will give you information you need
     Response r = Response
                .status(Response.Status.BAD_REQUEST)
                .entity(set any object your want to return)
                .build();


    return r;
}

add the following to your spring file

    <bean id="exceptionResponseBuilder" class="com.orgname.project.provider.ExceptionResponseBuilder">
   <property name="printStackTrace" value="true" />
</bean>
        <jaxrs:providers>
        <ref bean="exceptionResponseBuilder"/>
    </jaxrs:providers>

Upvotes: 2

Ricardo Gladwell
Ricardo Gladwell

Reputation: 3785

You need to enable the printStackTrack feature on the WebApplicationExceptionMapper class. You can do this by adding the following to your Spring XML context:

<jaxrs:providers>
  <bean class="org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper">
    <property name="printStackTrace" value="true" />
  </bean>
</jaxrs:providers>

Exceptions should now be logged to the log4j WARN channel.

Upvotes: 0

Related Questions