Log Restlet ClientResource request text

535 Views Asked by At

We're using Restlet 2.1.6 on the client side to communicate with a server built on Restlet as well. Try as we might we cannot manage to log the text (JSON or XML) or even the whole HTTP request we're about to send to the server. We use Slf4j over Log4j as our logging facility.

We've configured Restlet with its Slf4jLoggerFacade by setting a system property in the Spring context:

<bean id="systemPrereqs"
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" value="#{@systemProperties}" />
    <property name="targetMethod" value="putAll" />
    <property name="arguments">
        <!-- The new Properties -->
        <util:properties>
            <prop key="org.restlet.engine.loggerFacadeClass">org.restlet.ext.slf4j.Slf4jLoggerFacade</prop>
        </util:properties>
    </property>
</bean>

Our Log4j configuration for Restlet looks like this:

log4j.logger.org.restlet=debug

We're pretty sure that the system property is working since the log does contain new entries, it just doesn't contain the detail we need. Here's an excerpt of what's being output to the log by Restlet:

2013-12-11 09:52:10,860  INFO taskScheduler-1 org.restlet:141 - Starting the HTTP client
2013-12-11 09:52:10,961 DEBUG taskScheduler-1 org.restlet:96 - The length of the message body is unknown. The entity must be handled carefully and consumed entirely in order to surely release the connection.
2013-12-11 09:52:14,716  INFO Finalizer org.restlet:141 - Stopping the HTTP client

Right now we're logging in an expensive way by extracting the Representation into a temporary String, logging it, and then returning a new Representation. We have to do it this way since a Representation may be Stream-based and consumed by reading it.

private Representation logIt(Representation representation) {
    String representationText = representation.getText();
    logger.trace(representationText);
    representation = new StringRepresentation(representationText);
    return representation;
}

How can we get Restlet to log the actual Request/Response text from the client side and avoid the String copying and Representation re-construction?

0

There are 0 best solutions below