Skip to content

Commit

Permalink
Using traditional SLF4J api to work with Jenkins plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
diemol committed Nov 2, 2023
1 parent 4ad9e89 commit a3dc28b
Show file tree
Hide file tree
Showing 4 changed files with 632 additions and 602 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.saucelabs</groupId>
<artifactId>saucerest</artifactId>
<version>2.3.2-SNAPSHOT</version>
<version>2.3.3-SNAPSHOT</version>
<name>saucerest</name>
<description>Java library which provides helper methods for invoking the Sauce Labs REST API
</description>
Expand Down
68 changes: 28 additions & 40 deletions src/main/java/com/saucelabs/saucerest/api/AbstractEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,27 +241,27 @@ protected Response makeRequest(Request request) throws IOException {
}

if (shouldRetryOnHttpError(response)) {
LOGGER.atDebug()
.addArgument(request::method)
.addArgument(request::url)
.addArgument(response::code)
.log("Retrying request {} {} because of HTTP error {}");
LOGGER.debug(
"Retrying request {} {} because of HTTP error {}",
request.method(),
request.url(),
response.code());
response = retryRequest(request);
} else {
LOGGER.atTrace()
.addArgument(request::method)
.addArgument(request::url)
.addArgument(response::code)
.log("Not retrying request {} {} because of HTTP error {}");
LOGGER.trace(
"Not retrying request {} {} because of HTTP error {}",
request.method(),
request.url(),
response.code());
}

if (!response.isSuccessful()) {
LOGGER.atWarn()
.addArgument(request::method)
.addArgument(request::url)
.addArgument(response::code)
.addArgument(response::message)
.log("Request {} {} failed with response code {} and message \"{}\"");
LOGGER.warn(
"Request {} {} failed with response code {} and message \"{}\"",
request.method(),
request.url(),
response.code(),
response.message());
responseHandler(this, response);
}

Expand Down Expand Up @@ -290,10 +290,7 @@ private boolean shouldRetryOnHttpError(Response response) {
private Response retryRequest(Request request) throws IOException {
Response response;
try {
LOGGER.atDebug()
.addArgument(request::method)
.addArgument(request::url)
.log("Retrying request {} {}");
LOGGER.debug("Retrying request {} {}", request.method(), request.url());
response =
Failsafe.with(
new RetryPolicy<>()
Expand All @@ -304,9 +301,9 @@ private Response retryRequest(Request request) throws IOException {
.onRetry(
e -> {
if (e.getLastFailure() != null) {
LOGGER.atWarn()
.addArgument(() -> e.getLastFailure().getClass().getSimpleName())
.log("Retrying because of: {}");
LOGGER.warn(
"Retrying because of: {}",
e.getLastFailure().getClass().getSimpleName());
} else {
LOGGER.warn("Retrying");
}
Expand All @@ -316,10 +313,7 @@ private Response retryRequest(Request request) throws IOException {
LOGGER.error("Error retrying request", e);
throw new IOException(String.format("Error retrying request: %s", e.getMessage()), e);
}
LOGGER.atDebug()
.addArgument(request::method)
.addArgument(request::url)
.log("Request {} {} succeeded after retry");
LOGGER.debug("Request {} {} succeeded after retry", request.method(), request.url());
return response;
}

Expand All @@ -344,10 +338,8 @@ protected <T> T deserializeJSONObject(String jsonResponse, Class<T> clazz) throw
throw new IOException(
"Error deserializing JSON response to " + clazz.getSimpleName() + " class", e);
} catch (JsonDataException e) {
LOGGER.atWarn()
.addArgument(System::lineSeparator)
.addArgument(jsonResponse)
.log("Could not deserialize JSON response:{}{}");
LOGGER.warn(
"Could not deserialize JSON response: {}{}", System.lineSeparator(), jsonResponse);
throw e;
}
}
Expand Down Expand Up @@ -380,10 +372,8 @@ protected <T> List<T> deserializeJSONObject(String jsonResponse, List<Class<? ex
throw new IOException(
"Error deserializing JSON response to " + clazz.get(0).getSimpleName() + " class", e);
} catch (JsonDataException e) {
LOGGER.atWarn()
.addArgument(System::lineSeparator)
.addArgument(jsonResponse)
.log("Could not deserialize JSON response:{}{}");
LOGGER.warn(
"Could not deserialize JSON response: {}{}", System.lineSeparator(), jsonResponse);
throw e;
}
}
Expand Down Expand Up @@ -417,10 +407,8 @@ protected <T> List<T> deserializeJSONArray(String jsonResponse, Class<T> clazz)
throw new IOException(
"Error deserializing JSON response to " + clazz.getSimpleName() + " class", e);
} catch (JsonDataException e) {
LOGGER.atWarn()
.addArgument(System::lineSeparator)
.addArgument(jsonResponse)
.log("Could not deserialize JSON response:{}{}");
LOGGER.warn(
"Could not deserialize JSON response: {}{}", System.lineSeparator(), jsonResponse);
throw e;
}
}
Expand All @@ -437,7 +425,7 @@ protected void downloadFile(String url, String path, String fileName) {
try (BufferedSink sink = Okio.buffer(Okio.sink(Paths.get(path, fileName).toFile()))) {
sink.writeAll(Objects.requireNonNull(request(url, HttpMethod.GET).body()).source());
} catch (IOException e) {
LOGGER.error("Error downloading file to {} with filename {}", path, fileName, e);
LOGGER.error("Error downloading file to {} with filename {}", path, fileName, e);
}
}

Expand Down
Loading

0 comments on commit a3dc28b

Please sign in to comment.