Search in sources :

Example 1 with ClientLogger

use of com.azure.android.core.logging.ClientLogger in project azure-sdk-for-android by Azure.

the class HttpLoggingPolicy method process.

@Override
public void process(HttpPipelinePolicyChain chain) {
    // No logging will be performed, trigger a no-op.
    if (httpLogDetailLevel == HttpLogDetailLevel.NONE) {
        chain.processNextPolicy(chain.getRequest());
        return;
    }
    // final ClientLogger logger = new ClientLogger((String) context.getData("caller-method").orElse(""));
    // TODO: bring context ^
    final ClientLogger logger = new ClientLogger((String) "caller-method");
    if (!logger.canLogAtLevel(LogLevel.INFORMATIONAL)) {
        chain.processNextPolicy(chain.getRequest());
        return;
    }
    final long startNs = System.nanoTime();
    final HttpRequest httpRequest = chain.getRequest();
    // final Integer retryCount = context.getData(RETRY_COUNT_CONTEXT);
    // final Integer retryCount = null; // TODO: bring context ^
    StringBuilder requestLogMessage = new StringBuilder();
    if (httpLogDetailLevel.shouldLogUrl()) {
        requestLogMessage.append("--> ").append(httpRequest.getHttpMethod()).append(" ").append(this.getRedactedUrl(httpRequest.getUrl())).append(LINE_SEPARATOR);
    // if (retryCount != null) {
    // requestLogMessage.append(retryCount).append(LINE_SEPARATOR);
    // }
    }
    this.appendHeaders(logger, httpRequest.getHeaders(), requestLogMessage);
    if (httpLogDetailLevel.shouldLogBody()) {
        if (httpRequest.getBody() == null) {
            requestLogMessage.append("(empty body)").append(LINE_SEPARATOR).append("--> END ").append(httpRequest.getHttpMethod()).append(LINE_SEPARATOR);
        } else {
            final String requestContentType = httpRequest.getHeaders().getValue("Content-Type");
            final long requestContentLength = this.getContentLength(logger, httpRequest.getHeaders());
            if (this.isContentLoggable(requestContentType, requestContentLength)) {
                final String content = this.convertBytesToString(httpRequest.getBody(), logger);
                requestLogMessage.append(requestContentLength).append("-byte body:").append(LINE_SEPARATOR).append(content).append(LINE_SEPARATOR).append("--> END ").append(httpRequest.getHttpMethod()).append(LINE_SEPARATOR);
            } else {
                requestLogMessage.append(requestContentLength).append("-byte body: (content not logged)").append(LINE_SEPARATOR).append("--> END ").append(httpRequest.getHttpMethod()).append(LINE_SEPARATOR);
            }
        }
    }
    logger.info(requestLogMessage.toString());
    chain.processNextPolicy(httpRequest, new NextPolicyCallback() {

        @Override
        public PolicyCompleter.CompletionState onSuccess(HttpResponse response, PolicyCompleter completer) {
            long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
            final String contentLengthHeaderValue = response.getHeaderValue("Content-Length");
            final String contentLengthMessage = (contentLengthHeaderValue == null || contentLengthHeaderValue.length() == 0) ? "unknown-length body" : contentLengthHeaderValue + "-byte body";
            StringBuilder responseLogMessage = new StringBuilder();
            if (httpLogDetailLevel.shouldLogUrl()) {
                responseLogMessage.append("<-- ").append(response.getStatusCode()).append(" ").append(getRedactedUrl(response.getRequest().getUrl())).append(" (").append(tookMs).append(" ms, ").append(contentLengthMessage).append(")").append(LINE_SEPARATOR);
            }
            appendHeaders(logger, response.getHeaders(), responseLogMessage);
            HttpResponse httpResponse = response;
            if (httpLogDetailLevel.shouldLogBody()) {
                final String responseContentType = response.getHeaderValue("Content-Type");
                final long responseContentLength = getContentLength(logger, response.getHeaders());
                if (isContentLoggable(responseContentType, responseContentLength)) {
                    httpResponse = response.buffer();
                    final String content = convertBytesToString(httpResponse.getBodyAsByteArray(), logger);
                    responseLogMessage.append("Response body:").append(LINE_SEPARATOR).append(content).append(LINE_SEPARATOR).append("<-- END HTTP");
                } else {
                    responseLogMessage.append("(body content not logged)").append(LINE_SEPARATOR).append("<-- END HTTP");
                }
            } else {
                responseLogMessage.append("<-- END HTTP");
            }
            logger.info(responseLogMessage.toString());
            return completer.completed(httpResponse);
        }

        @Override
        public PolicyCompleter.CompletionState onError(Throwable error, PolicyCompleter completer) {
            logger.warning("<-- HTTP FAILED: ", error);
            return completer.completedError(error);
        }
    });
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) ClientLogger(com.azure.android.core.logging.ClientLogger) HttpResponse(com.azure.android.core.http.HttpResponse) NextPolicyCallback(com.azure.android.core.http.NextPolicyCallback) PolicyCompleter(com.azure.android.core.http.PolicyCompleter)

Example 2 with ClientLogger

use of com.azure.android.core.logging.ClientLogger in project azure-sdk-for-android by Azure.

the class TestBase method initializeTestMode.

private static TestMode initializeTestMode() {
    final ClientLogger logger = new ClientLogger(TestBase.class);
    final String azureTestMode = getConfig(AZURE_TEST_MODE);
    if (azureTestMode != null) {
        try {
            return TestMode.valueOf(azureTestMode.toUpperCase(Locale.US));
        } catch (IllegalArgumentException e) {
            logger.error("Could not parse '{}' into TestEnum. Using 'Playback' mode.", azureTestMode);
            return TestMode.PLAYBACK;
        }
    }
    logger.info("Environment variable '{}' has not been set yet. Using 'Playback' mode.", AZURE_TEST_MODE);
    return TestMode.PLAYBACK;
}
Also used : ClientLogger(com.azure.android.core.logging.ClientLogger)

Example 3 with ClientLogger

use of com.azure.android.core.logging.ClientLogger in project azure-sdk-for-android by Azure.

the class ChatImplClientTestBase method initializeTestMode.

private static TestMode initializeTestMode() {
    ClientLogger logger = new ClientLogger(ChatImplClientTestBase.class);
    String azureTestMode = getConfig("AZURE_TEST_MODE");
    if (azureTestMode != null) {
        System.out.println("azureTestMode: " + azureTestMode);
        try {
            return TestMode.valueOf(azureTestMode.toUpperCase(Locale.US));
        } catch (IllegalArgumentException var3) {
            logger.error("Could not parse '{}' into TestEnum. Using 'Playback' mode.", azureTestMode);
            return TestMode.PLAYBACK;
        }
    } else {
        logger.info("Environment variable '{}' has not been set yet. Using 'Playback' mode.", "AZURE_TEST_MODE");
        return TestMode.PLAYBACK;
    }
}
Also used : ClientLogger(com.azure.android.core.logging.ClientLogger)

Example 4 with ClientLogger

use of com.azure.android.core.logging.ClientLogger in project azure-sdk-for-android by Azure.

the class ChatClientTestBase method initializeTestMode.

private static TestMode initializeTestMode() {
    ClientLogger logger = new ClientLogger(ChatClientTestBase.class);
    String azureTestMode = getConfig("AZURE_TEST_MODE");
    if (azureTestMode != null) {
        System.out.println("azureTestMode: " + azureTestMode);
        try {
            return TestMode.valueOf(azureTestMode.toUpperCase(Locale.US));
        } catch (IllegalArgumentException var3) {
            logger.error("Could not parse '{}' into TestEnum. Using 'Playback' mode.", azureTestMode);
            return TestMode.PLAYBACK;
        }
    } else {
        logger.info("Environment variable '{}' has not been set yet. Using 'Playback' mode.", "AZURE_TEST_MODE");
        return TestMode.PLAYBACK;
    }
}
Also used : ClientLogger(com.azure.android.core.logging.ClientLogger)

Aggregations

ClientLogger (com.azure.android.core.logging.ClientLogger)4 HttpRequest (com.azure.android.core.http.HttpRequest)1 HttpResponse (com.azure.android.core.http.HttpResponse)1 NextPolicyCallback (com.azure.android.core.http.NextPolicyCallback)1 PolicyCompleter (com.azure.android.core.http.PolicyCompleter)1