private void log(String message) { logger.trace(() -> message); }
private void log(final Supplier<Object> message) { SphereInternalLogger.getLogger(this.getClass()).trace(message); }
private void log(final Supplier<Object> message) { SphereInternalLogger.getLogger(this.getClass()).trace(message); }
private void log(final Supplier<Object> message) { SphereInternalLogger.getLogger(this.getClass()).trace(message); }
private void log(final Supplier<Object> message) { SphereInternalLogger.getLogger(this.getClass()).trace(message); }
private static <T> T processHttpResponse(final SphereRequest<T> sphereRequest, final ObjectMapper objectMapper, final SphereApiConfig config, final HttpResponse httpResponse) { final SphereInternalLogger logger = getLogger(httpResponse); logger.debug(() -> httpResponse); logger.trace(() -> httpResponse.getStatusCode() + "\n" + Optional.ofNullable(httpResponse.getResponseBody()).map(body -> SphereJsonUtils.prettyPrint(bytesToString(body))).orElse("No body present.") + "\n"); final List<String> notices = httpResponse.getHeaders().getHeadersAsMap().get(SphereHttpHeaders.X_DEPRECATION_NOTICE); if (notices != null) { notices.stream().forEach(message -> logger.warn(() -> "Deprecation notice : " + message)); } return parse(sphereRequest, objectMapper, config, httpResponse); }
public void showLazyToString() { BigComplexObjectWithExpensiveToString object = new BigComplexObjectWithExpensiveToString(); //object.toString will only be called if trace is enabled LOGGER.trace(() -> "failed doing xyz with " + object); } }
public static void logRequestData(final Http.Context ctx) { final List<ReportRawData> rawDatas = (List<ReportRawData>) ctx.args.get(KEY); if (rawDatas != null && rawDatas instanceof List) { final String durations = rawDatas.stream().map(data -> data.getStopTimestamp() - data.getStartTimestamp()).map(l -> Long.toString(l) + " ms").collect(joining(", ")); if (LOGGER.isTraceEnabled()) { final String queriesAsString = rawDatas.stream().map(q -> { final String url = q.getHttpRequest().getUrl(); final String[] split = StringUtils.split(url, "/", 4); final String shortUrl = split.length == 4 ? "/" + split[3] : url; final HttpMethod httpMethod = q.getHttpRequest().getHttpMethod(); final long duration = q.getStopTimestamp() - q.getStartTimestamp(); final Integer bodySize = getBodySize(q); return format(" %s %s %dms %dbytes", httpMethod, shortUrl, duration, bodySize); }).collect(joining("\n")); LOGGER.trace(() -> format("commercetools requests in %s: \n%s", ctx.request(), queriesAsString)); } else { final Pair<List<ReportRawData>, List<ReportRawData>> queryCommandPair = splitByQueriesAndCommands(rawDatas); final List<ReportRawData> queries = queryCommandPair.getLeft(); final List<ReportRawData> commands = queryCommandPair.getRight(); final int size = calculateTotalSize(rawDatas); LOGGER.debug(() -> format("%s used %d requests (%d queries, %d commands, %dbytes fetched, in (%s)).", ctx.request(), rawDatas.size(), queries.size(), commands.size(), size, durations)); } } }
private static <T> T processHttpResponse(final SphereRequest<T> sphereRequest, final ObjectMapper objectMapper, final SphereApiConfig config, final HttpResponse httpResponse, final HttpRequest httpRequest) { final SphereInternalLogger logger = getLogger(httpResponse); logger.debug(() -> httpResponse); logger.trace(() -> httpResponse.getStatusCode() + "\n" + Optional.ofNullable(httpResponse.getResponseBody()).map(body -> SphereJsonUtils.prettyPrint(bytesToString(body))).orElse("No body present.")); final List<String> notices = httpResponse.getHeaders().getHeadersAsMap().get(SphereHttpHeaders.X_DEPRECATION_NOTICE); if (notices != null) { notices.forEach(message -> logger.warn(() -> "Deprecation notice : " + message)); } return parse(sphereRequest, objectMapper, config, httpResponse, httpRequest); }
static <T> Function<HttpResponse, T> preProcess(final SphereRequest<T> sphereRequest, final ObjectMapper objectMapper, final SphereApiConfig config) { return httpResponse -> { final SphereInternalLogger logger = getLogger(httpResponse); logger.debug(() -> httpResponse); logger.trace(() -> httpResponse.getStatusCode() + "\n" + httpResponse.getResponseBody().map(body -> JsonUtils.prettyPrintJsonStringSecure(bytesToString(body))).orElse("No body present.") + "\n"); final List<String> notices = httpResponse.getHeaders().getHeadersAsMap().get(SphereHttpHeaders.X_DEPRECATION_NOTICE); if (notices != null) { notices.stream().forEach(message -> logger.warn(() -> "Deprecation notice : " + message)); } return parse(httpResponse, sphereRequest, objectMapper, config); }; }
private static <T> T processHttpResponse(final SphereRequest<T> sphereRequest, final ObjectMapper objectMapper, final SphereApiConfig config, final HttpResponse httpResponse, final HttpRequest httpRequest) { final SphereInternalLogger logger = getLogger(httpResponse); logger.debug(() -> httpResponse); logger.trace(() -> httpResponse.getStatusCode() + "\n" + Optional.ofNullable(httpResponse.getResponseBody()).map(body -> SphereJsonUtils.prettyPrint(bytesToString(body))).orElse("No body present.")); final List<String> notices = httpResponse.getHeaders().getHeadersAsMap().get(SphereHttpHeaders.X_DEPRECATION_NOTICE); if (notices != null) { notices.forEach(message -> logger.warn(() -> "Deprecation notice : " + message)); } return parse(sphereRequest, objectMapper, config, httpResponse, httpRequest); }
@Override public <T> CompletableFuture<T> execute(final ClientRequest<T> clientRequest) { final SphereInternalLogger logger = getLogger(clientRequest); logger.debug(() -> clientRequest); logger.trace(() -> { final Optional<String> requestBody = clientRequest.httpRequest().getBody(); return requestBody.map(body -> JsonUtils.prettyPrintJsonStringSecure(body)).orElse("no request body present"); }); return requestExecutor. execute(clientRequest). thenApply(preProcess(clientRequest, clientRequest.resultMapper())); }
private <T> CompletableFuture<T> execute(final SphereRequest<T> sphereRequest, final String token) { final HttpRequest httpRequest = createHttpRequest(sphereRequest, token); final SphereInternalLogger logger = getLogger(httpRequest); logger.debug(() -> sphereRequest); logger.trace(() -> { final String output; if (httpRequest.getBody().isPresent() && httpRequest.getBody().get() instanceof StringHttpRequestBody) { final StringHttpRequestBody body = (StringHttpRequestBody) httpRequest.getBody().get(); final String unformattedJson = body.getString(); output = "send: " + unformattedJson + "\nformatted: " + JsonUtils.prettyPrintJsonStringSecure(unformattedJson); } else { output = "no request body present"; } return output; }); return httpClient. execute(httpRequest). thenApply(preProcess(sphereRequest, objectMapper, config)); }
@Override public T apply(final HttpResponse httpResponse) { final SphereInternalLogger logger = getLogger(httpResponse); logger.debug(() -> httpResponse.withoutRequest()); logger.trace(() -> httpResponse.getStatusCode() + "\n" + JsonUtils.prettyPrintJsonStringSecure(httpResponse.getResponseBody()) + "\n"); final int status = httpResponse.getStatusCode(); final String body = httpResponse.getResponseBody(); final boolean hasError = status / 100 != 2; if (hasError) { SphereErrorResponse errorResponse; try { if (Strings.isNullOrEmpty(body)) {//the /model/id endpoint does not return JSON on 404 errorResponse = new SphereErrorResponse(status, "<no body>", Collections.<SphereError>emptyList()); } else { errorResponse = objectMapper.readValue(body, errorResponseJsonTypeRef); } } catch (final Exception e) { // This can only happen when the backend and SDK don't match. final SphereException exception = new SphereException("Can't parse backend response", e); fillExceptionWithData(httpResponse, exception, clientRequest); throw exception; } final SphereBackendException exception = new SphereBackendException(clientRequest.httpRequest().getPath(), errorResponse); fillExceptionWithData(httpResponse, exception, clientRequest); throw exception; } else { return underlying.apply(httpResponse); } } };
private <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest, final String token, final int ttl) { final HttpRequest httpRequest = createHttpRequest(sphereRequest, token); final SphereInternalLogger logger = getLogger(httpRequest); logger.debug(() -> sphereRequest); logger.trace(() -> { final String output; final String httpMethodAndUrl = httpRequest.getHttpMethod() + " " + httpRequest.getUrl(); if (httpRequest.getBody() != null && httpRequest.getBody() instanceof StringHttpRequestBody) { final StringHttpRequestBody body = (StringHttpRequestBody) httpRequest.getBody(); final String unformattedBody = body.getSecuredBody(); final boolean isJsonRequest = httpRequest.getHeaders().findFlatHeader(HttpHeaders.CONTENT_TYPE).map(ct -> ct.toLowerCase().contains("json")).orElse(true); if (isJsonRequest) { String prettyPrint; try { prettyPrint = SphereJsonUtils.prettyPrint(unformattedBody); } catch (final JsonException e) { classLogger.warn("pretty print failed", e); prettyPrint = unformattedBody; } output = "send: " + httpMethodAndUrl + "\nformatted: " + prettyPrint; } else { output = "send: " + httpRequest.getHttpMethod() + " " + httpRequest.getUrl() + " " + unformattedBody; } } else { output = httpMethodAndUrl + " <no body>"; } return output; }); return executeWithRecover(sphereRequest, httpRequest, ttl); }
private <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest, final String token, final int ttl) { final HttpRequest httpRequest = createHttpRequest(sphereRequest, token); final SphereInternalLogger logger = getLogger(httpRequest); logger.debug(() -> sphereRequest); logger.trace(() -> { final String output; final String httpMethodAndUrl = httpRequest.getHttpMethod() + " " + httpRequest.getUrl(); if (httpRequest.getBody() != null && httpRequest.getBody() instanceof StringHttpRequestBody) { final StringHttpRequestBody body = (StringHttpRequestBody) httpRequest.getBody(); final String unformattedBody = body.getSecuredBody(); final boolean isJsonRequest = httpRequest.getHeaders().findFlatHeader(HttpHeaders.CONTENT_TYPE).map(ct -> ct.toLowerCase().contains("json")).orElse(true); if (isJsonRequest) { String prettyPrint; try { prettyPrint = SphereJsonUtils.prettyPrint(unformattedBody); } catch (final JsonException e) { classLogger.warn("pretty print failed", e); prettyPrint = unformattedBody; } output = "send: " + httpMethodAndUrl + "\nformatted: " + prettyPrint; } else { output = "send: " + httpRequest.getHttpMethod() + " " + httpRequest.getUrl() + " " + unformattedBody; } } else { output = httpMethodAndUrl + " <no body>"; } return output; }); return executeWithRecover(sphereRequest, httpRequest, ttl); }
private <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest, final String token) { final HttpRequest httpRequest = createHttpRequest(sphereRequest, token); final SphereInternalLogger logger = getLogger(httpRequest); logger.debug(() -> sphereRequest); logger.trace(() -> { final String output; if (httpRequest.getBody() != null && httpRequest.getBody() instanceof StringHttpRequestBody) { final StringHttpRequestBody body = (StringHttpRequestBody) httpRequest.getBody(); final String unformattedJson = body.getSecuredBody(); output = "send: " + unformattedJson + "\nformatted: " + SphereJsonUtils.prettyPrint(unformattedJson); } else { output = "no request body present"; } return output; }); return httpClient.execute(httpRequest).thenApply(httpResponse -> { try { return processHttpResponse(sphereRequest, objectMapper, config, httpResponse); } catch (final SphereException e) { fillExceptionWithData(sphereRequest, httpResponse, e, config); throw e; } }); }