How to format logs and add elapsed time with LoggingHandler in Spring Integration
<p>I am using Spring Integration DSL with Java 1.7 and AMQP, and I am trying to log my events with LoggingHandler through logback, this is a snippet of my configuration code.</p>

<pre><code>@Bean
public IntegrationFlow inboundFlow() {
    return IntegrationFlows.from(Amqp.inboundGateway(simpleMessageListenerContainer())
            .mappedReplyHeaders(AMQPConstants.AMQP_CUSTOM_HEADER_FIELD_NAME_MATCH_PATTERN)
            .mappedRequestHeaders(AMQPConstants.AMQP_CUSTOM_HEADER_FIELD_NAME_MATCH_PATTERN)
            .errorChannel(gatewayErrorChannel())
            .requestChannel(gatewayRequestChannel())
            .replyChannel(gatewayResponseChannel())
        )
        .transform(getCustomFromJsonTransformer())
        .route(new HeaderValueRouter(AMQPConstants.OPERATION_ROUTING_KEY))
        .get();
}

@Bean
public MessageChannel gatewayRequestChannel() {
    return MessageChannels.publishSubscribe().get();
}

@Bean
public MessageChannel gatewayResponseChannel() {
    return MessageChannels.publishSubscribe().get();
}

@Bean
public LoggingHandler getLoggingHandler(){
    LoggingHandler loggingHandler =  new LoggingHandler(LoggingHandler.Level.INFO.name());
    loggingHandler.setLoggerName("analytics");
    loggingHandler.setExpression("headers");
    return loggingHandler;
}

private IntegrationFlow fileLoggerOutboundFlowTemplate(MessageChannel fromMessageChannel) {
    return IntegrationFlows.from(fromMessageChannel)
        .handle(getLoggingHandler())
        .get();
}

@Bean
public IntegrationFlow requestFileLoggerOutboundFlow() {
    return fileLoggerOutboundFlowTemplate(gatewayRequestChannel());
}

@Bean
public IntegrationFlow responseFileLoggerOutboundFlow() {
    return fileLoggerOutboundFlowTemplate(gatewayResponseChannel());
}
</code></pre>

<p>Now, I have two main problems here:</p>

<ol>
<li><p>This loggingHandler is logging headers from request and response to logback, an example for a response log entry with headers I currently have, look like this :</p>

<pre><code>{errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@ff940b1, replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@ff940b1, contentType=JSON, amqp_consumerQueue=clientAdapter, userId=12345678, monitoringId=34841e03-a7f8-4f0b-8860-2f94c94e2c7a, timestamp=1454460846221, id=799de4c1-6167-6f45-de4d-1964d357d2dc, adapterRoutingKey=clientRoutingKey, messageType=RESPONSE, operationRoutingKey=login, amqp_consumerTag=amq.ctag-YMP8rrbTxLAXdYsFJ7HVjQ, result=OK, metaData={"clientVersion":"1.2.1","clientType":"WEB","ipAddress":"127.0.0.1"}}
</code></pre></li>
</ol>

<p>So, the question here is, is there a way to format this output to kind of CSV format using an expression? This make me think that maybe this could be solved with a logback layout.</p>

<ol start="2">
<li>This log events happen in two different times, with request and response, is there a way to log elapsed time between request and response? I was using Splunk to process both log entries and calculate elapsed time but I would like to have this in one log line and avoid Splunk because license cost.</li>
</ol>

<p>Thanks for your help.</p>

<p>UPDATE:</p>

<p>At the end, I used what Artem Bilan proposed with <code>enrichHeaders()</code>, I added timestamp in the request and when response is going to send back calculate elapsed time.</p>

<p>Also I have to say, that this requirement about logging in CSV format is more than just logging, so decided to handle this in a different way using AMQP to track every transaction and have it in the desired format.</p>