Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add SLF4J logging to fullstack-helm-client module #137

Merged
merged 13 commits into from
Jul 5, 2023
2 changes: 2 additions & 0 deletions fullstack-bom/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ dependencies {
api(platform("org.assertj:assertj-bom:3.24.2"))
api(platform("com.fasterxml.jackson:jackson-bom:2.15.2"))
api(platform("org.mockito:mockito-bom:5.3.1"))
api(platform("org.apache.logging.log4j:log4j:2.20.0"))
api(platform("org.apache.logging.log4j:log4j-slf4j2-impl:2.20.0"))
nathanklick marked this conversation as resolved.
Show resolved Hide resolved
}

dependencies.constraints {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Represents the execution of a helm command and is responsible for parsing the response.
*/
public final class HelmExecution {
private static final Logger LOGGER = LoggerFactory.getLogger(HelmExecution.class);

/**
* The message for a timeout error.
Expand Down Expand Up @@ -210,8 +213,20 @@ public <T> T responseAs(final Class<T> responseClass, final Duration timeout) {
throw new HelmExecutionException(exitCode());
}

final String standardOutput = StreamUtils.streamToString(suppressExceptions(this::standardOutput));
final String standardError = StreamUtils.streamToString(suppressExceptions(this::standardError));

LOGGER.atDebug()
.setMessage(
"responseAs exiting with exitCode: {}\n\tResponseClass: {}\n\tstandardOutput: {}\n\tstandardError: {}")
.addArgument(this::exitCode)
.addArgument(responseClass.getName())
.addArgument(standardOutput)
.addArgument(standardError)
.log();

try {
return OBJECT_MAPPER.readValue(standardOutput(), responseClass);
return OBJECT_MAPPER.readValue(standardOutput, responseClass);
} catch (final Exception e) {
throw new HelmParserException(String.format(MSG_DESERIALIZATION_ERROR, responseClass.getName()), e);
nathanklick marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down Expand Up @@ -258,10 +273,22 @@ public <T> List<T> responseAsList(final Class<T> responseClass, final Duration t
throw new HelmExecutionException(exitCode());
}

final String standardOutput = StreamUtils.streamToString(suppressExceptions(this::standardOutput));
final String standardError = StreamUtils.streamToString(suppressExceptions(this::standardError));

LOGGER.atDebug()
.setMessage(
"ResponseAsList exiting with exitCode: {}\n\tResponseClass: {}\n\tstandardOutput: {}\n\tstandardError: {}")
.addArgument(this::exitCode)
.addArgument(responseClass.getName())
.addArgument(standardOutput)
.addArgument(standardError)
.log();

try {
return OBJECT_MAPPER
.readerFor(responseClass)
.<T>readValues(standardOutput())
.<T>readValues(standardOutput)
.readAll();
} catch (final Exception e) {
throw new HelmParserException(String.format(MSG_LIST_DESERIALIZATION_ERROR, responseClass.getName()), e);
Expand Down Expand Up @@ -296,11 +323,18 @@ public void call(final Duration timeout) {
return;
}

final String standardOutput = StreamUtils.streamToString(suppressExceptions(this::standardOutput));
final String standardError = StreamUtils.streamToString(suppressExceptions(this::standardError));

LOGGER.atDebug()
.setMessage("Call exiting with exitCode: {}\n\tstandardOutput: {}\n\tstandardError: {}")
.addArgument(this::exitCode)
.addArgument(standardOutput)
.addArgument(standardError)
.log();

if (exitCode() != 0) {
throw new HelmExecutionException(
exitCode(),
StreamUtils.streamToString(suppressExceptions(this::standardError)),
StreamUtils.streamToString(suppressExceptions(this::standardOutput)));
throw new HelmExecutionException(exitCode(), standardError, standardOutput);
nathanklick marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A builder for creating a helm command execution.
*/
public final class HelmExecutionBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(HelmExecutionBuilder.class);

/**
* The path to the helm executable.
Expand Down Expand Up @@ -194,6 +197,13 @@ private String[] buildCommand() {
}

command.addAll(positionals);
return command.toArray(new String[0]);

String[] commandArray = command.toArray(new String[0]);
LOGGER.atDebug()
.setMessage("Helm command: {}")
.addArgument(String.join(" ", Arrays.copyOfRange(commandArray, 1, commandArray.length)))
.log();

return commandArray;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
import com.hedera.fullstack.helm.client.HelmConfigurationException;
import java.io.IOException;
import java.nio.file.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Loads the Helm executable contained in the JAR file into a temporary directory.
*/
public final class HelmSoftwareLoader {
private static final Logger LOGGER = LoggerFactory.getLogger(HelmSoftwareLoader.class);

/**
* The root resources folder where the software is located.
Expand Down Expand Up @@ -83,6 +86,13 @@ public static Path installSupportedVersion() {
pathBuilder.append(".exe");
}

LOGGER.atDebug()
.setMessage("Loading Helm executable from JAR file. [os={}, arch={}, path={}]")
.addArgument(os.name())
.addArgument(arch.name())
.addArgument(pathBuilder.toString())
.log();

return RESOURCE_LOADER.load(pathBuilder.toString());
} catch (IOException | SecurityException | IllegalStateException e) {
throw new HelmConfigurationException(e);
Expand Down
1 change: 1 addition & 0 deletions fullstack-helm-client/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
com.fasterxml.jackson.databind;

requires com.fasterxml.jackson.databind;
requires org.slf4j;
requires transitive com.hedera.fullstack.base.api;
requires transitive com.fasterxml.jackson.annotation;
}
32 changes: 32 additions & 0 deletions fullstack-helm-client/src/test/resources/log4j2-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2016-2022 Hedera Hashgraph, LLC
~
~ This software is the confidential and proprietary information of
~ Hedera Hashgraph, LLC. ("Confidential Information"). You shall not
~ disclose such Confidential Information and shall use it only in
~ accordance with the terms of the license agreement you entered into
~ with Hedera Hashgraph.
~
~ HEDERA HASHGRAPH MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
~ THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
~ TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
~ PARTICULAR PURPOSE, OR NON-INFRINGEMENT. HEDERA HASHGRAPH SHALL NOT BE LIABLE FOR
~ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
~ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
-->

<Configuration status="Warn">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-8sn %-5p %-16marker &lt;%t&gt; %c{1}: %msg%n</pattern>
</PatternLayout>
</Console>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>