summaryrefslogtreecommitdiffstats
path: root/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/LoggingAsyncHttpClient.java
blob: d4aff12146def2e2dae308f1c9483280d90997dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.utils.communication.http;

import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation;
import com.yahoo.vespa.clustercontroller.utils.communication.async.PipedAsyncOperation;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LoggingAsyncHttpClient<T extends HttpResult> extends AsyncHttpClientWithBase<T> {
    private static final Logger log = Logger.getLogger(LoggingAsyncHttpClient.class.getName());
    private int requestCounter = 0;

    public LoggingAsyncHttpClient(AsyncHttpClient<T> client) {
        super(client);
        log.info("Logging HTTP requests if fine logging level is added");
    }

    public AsyncOperation<T> execute(HttpRequest r) {
        final int requestCount = ++requestCounter;
        log.fine("Issuing HTTP request " + requestCount + ": " + r.toString(true));
        final AsyncOperation<T> op = client.execute(r);
        return new PipedAsyncOperation<T, T>(op) {
            @Override
            public T convertResult(T result) {
                if (log.isLoggable(Level.FINE)) {
                    if (op.isSuccess()) {
                        log.fine("HTTP request " + requestCount + " completed: " + result.toString(true));
                    } else {
                        StringWriter sw = new StringWriter();
                        op.getCause().printStackTrace(new PrintWriter(sw));
                        log.fine("HTTP request " + requestCount + " failed: " + sw);
                    }
                }
                return result;
            }
        };
    }
}