Skip to content

Commit

Permalink
feat: add SLF4J logging to fullstack-helm-client module (#137)
Browse files Browse the repository at this point in the history
Signed-off-by: Jeromy Cannon <jeromy@swirldslabs.com>
Signed-off-by: Nathan Klick <nathan@swirldslabs.com>
Co-authored-by: Nathan Klick <nathan@swirldslabs.com>
  • Loading branch information
jeromy-cannon and nathanklick authored Jul 5, 2023
1 parent 118662f commit 4115810
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ javaModuleDependencies {
}

extraJavaModuleInfo {
module("org.apache.logging.log4j:log4j-slf4j2-impl", "org.apache.logging.log4j.slf4j2.impl") {
exportAllPackages()
}

knownModule("org.slf4j:slf4j-api", "org.slf4j")
knownModule("org.slf4j:slf4j-nop", "org.slf4j.nop")
knownModule("org.slf4j:slf4j-simple", "org.slf4j.simple")
Expand Down
1 change: 1 addition & 0 deletions fullstack-bom/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ 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-bom:2.20.0"))
}

dependencies.constraints {
Expand Down
2 changes: 2 additions & 0 deletions fullstack-helm-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ dependencies {
testImplementation(gav("org.junit.jupiter.api"))
testImplementation(gav("org.junit.jupiter.params"))
testImplementation(gav("org.assertj.core"))
runtimeOnly(gav("org.apache.logging.log4j"))
runtimeOnly(gav("org.apache.logging.log4j.slf4j2.impl"))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@
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 {
/**
* The logger for this class which should be used for all logging.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(HelmExecution.class);

/**
* The message for a timeout error.
Expand Down Expand Up @@ -78,6 +84,11 @@ public final class HelmExecution {
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}

/**
* Creates a new {@link HelmExecution} instance for the specified process.
*
* @param process the underlying {@link Process} instance wrapped by this {@link HelmExecution} instance.
*/
public HelmExecution(final Process process) {
this.process = Objects.requireNonNull(process, "process must not be null");
this.standardOutputSink = new BufferedStreamSink(process.getInputStream());
Expand Down Expand Up @@ -210,9 +221,28 @@ 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) {
LOGGER.atWarn()
.setMessage("ResponseAs failed to deserialize response into class: {}\n\tresponse: {}")
.addArgument(responseClass.getName())
.addArgument(standardOutput)
.setCause(e)
.log();

throw new HelmParserException(String.format(MSG_DESERIALIZATION_ERROR, responseClass.getName()), e);
}
}
Expand Down Expand Up @@ -258,12 +288,31 @@ 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) {
LOGGER.atWarn()
.setMessage("ResponseAsList failed to deserialize response into class: {}\n\tresponse: {}")
.addArgument(responseClass.getName())
.addArgument(standardOutput)
.setCause(e)
.log();

throw new HelmParserException(String.format(MSG_LIST_DESERIALIZATION_ERROR, responseClass.getName()), e);
}
}
Expand Down Expand Up @@ -296,11 +345,25 @@ 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)));
LOGGER.atWarn()
.setMessage("Call exiting with exitCode: {}\n\tstandardOutput: {}\n\tstandardError: {}")
.addArgument(this::exitCode)
.addArgument(standardOutput)
.addArgument(standardError)
.log();

throw new HelmExecutionException(exitCode(), standardError, standardOutput);
}
}
}
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>

0 comments on commit 4115810

Please sign in to comment.