Log requests within client application based on PTV sdk (Java)

Deals with generic topics such as logging, framework and so on. If you are not sure where to place your topic just put it here.
Post Reply
User avatar
Bernd Welter
Site Admin
Posts: 2574
Joined: Mon Apr 14, 2014 10:28 am
Contact:

Log requests within client application based on PTV sdk (Java)

Post by Bernd Welter »

These days a partner asked us how to log the requests and responses from within an application that uses client classes shipped by PTV.
Now here's some piece of code provided by DEV (Thanks, Ralf!). The logging of request and response bodies can be achieved by using this snippet to create the xServer client in Java (xserver-java-openapi.client / JSON):

Code: Select all

import com.microsoft.rest.LogLevel;
import com.microsoft.rest.RestClient;
import com.microsoft.rest.ServiceResponseBuilder;
import com.microsoft.rest.serializer.JacksonAdapter
...
import com.ptvgroup.xserver.XServer
...
static void setUp() {
    RestClient restClient = new RestClient.Builder()
            .withBaseUrl(<replace_with_your_server_base_url>)
            .withResponseBuilderFactory(new ServiceResponseBuilder.Factory())
            .withSerializerAdapter(new JacksonAdapter())
            .withLogLevel(LogLevel.BODY)
            .build();
    xServerApi = new XServerImpl(restClient);
}
:!: Assert that your runtime has sl4j and and a logger impl like log4j2. You also need a logging config in your classpath. For log4j2 I used this log4j2.xml to log to console:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d;%p;%C{2}::%M[%L];%m%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Root level="WARN">
            <AppenderRef ref="Console"/>
        </Root>
        <Logger name="com.ptvgroup.xserver" level="INFO" additivity="false">
            <AppenderRef ref="Console"/>
        </Logger>
    </Loggers>
</Configuration>
Bernd
Bernd Welter
Technical Partner Manager Developer Components
PTV Logistics - Germany

Bernd at... The Forum,LinkedIn, Youtube, StackOverflow
I like the smell of PTV Developer in the morning... :twisted:
Post Reply