/*
 * Copyright 2013-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.cloudfoundry.reactor.util;

import org.cloudfoundry.util.TimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.netty.http.client.HttpClientRequest;
import reactor.netty.http.client.HttpClientResponse;

import java.util.List;

public class RequestLogger {

    static final Logger REQUEST_LOGGER = LoggerFactory.getLogger("cloudfoundry-client.request");

    static final Logger RESPONSE_LOGGER = LoggerFactory.getLogger("cloudfoundry-client.response");

    private static final String CF_WARNINGS = "X-Cf-Warnings";

    private long requestSentTime;

    public void request(HttpClientRequest request) {
        request(String.format("%-6s {}", request.method()), request.uri());
    }

    public void response(HttpClientResponse response) {
        if (!RESPONSE_LOGGER.isDebugEnabled()) {
            return;
        }

        String elapsed = TimeUtils.asTime(System.currentTimeMillis() - this.requestSentTime);
        List<String> warnings = response.responseHeaders().getAll(CF_WARNINGS);

        if (warnings.isEmpty()) {
            RESPONSE_LOGGER.debug("{}    {} ({})", response.status().code(), response.uri(), elapsed);
        } else {
            RESPONSE_LOGGER.warn("{}    {} ({}) [{}]", response.status().code(), response.uri(), elapsed, String.join(", ", warnings));
        }
    }

    public void websocketRequest(String uri) {
        request("WS     {}", uri);
    }

    private void request(String message, String uri) {
        REQUEST_LOGGER.debug(message, uri);
        this.requestSentTime = System.currentTimeMillis();
    }

}

@@ -1686,24 +1686,80 @@
 sEmpty()) %7B%0A
+            if (RESPONSE_LOGGER.isTraceEnabled()) %7B%0A    
             
@@ -1791,16 +1791,20 @@
     %7B%7D (
+%7B%7D, 
 %7B%7D)%22, re
@@ -1850,16 +1850,69 @@
  elapsed
+, response.responseHeaders().get(%22X-Vcap-Request-Id%22)
 );%0A     
@@ -1914,16 +1914,20 @@
         
+  
+  
 %7D else %7B
@@ -1927,16 +1927,448 @@
  else %7B%0A
+                RESPONSE_LOGGER.debug(%22%7B%7D    %7B%7D (%7B%7D)%22, response.status().code(), response.uri(), elapsed);%0A            %7D%0A        %7D else %7B%0A            if (RESPONSE_LOGGER.isTraceEnabled()) %7B%0A                RESPONSE_LOGGER.warn(%22%7B%7D    %7B%7D (%7B%7D, %7B%7D) %5B%7B%7D%5D%22, response.status().code(), response.uri(), elapsed, response.responseHeaders().get(%22X-Vcap-Request-Id%22),%0A                    String.join(%22, %22, warnings));%0A            %7D else %7B%0A    
         
@@ -2486,32 +2486,47 @@
 , %22, warnings));
+%0A%0A            %7D
 %0A        %7D%0A    %7D

Add Request ID Logging