Skip to content

Commit

Permalink
Merge pull request #348 from sghill-rewrite/refactor/logger
Browse files Browse the repository at this point in the history
  • Loading branch information
sghill authored Aug 18, 2023
2 parents 42ef3e9 + f923905 commit c720b95
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ CloseableHttpClient getHttpClient(PrintStream logger, URI stashServer, boolean i
HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(registry);
clientBuilder.setConnectionManager(connectionManager);
} catch (NoSuchAlgorithmException nsae) {
logger.println("Couldn't establish SSL context:");
nsae.printStackTrace(logger);
logger.println("Could not establish SSL context");
LOGGER.error("Could not establish SSL context", nsae);
} catch (KeyManagementException | KeyStoreException e) {
logger.println("Couldn't initialize SSL context:");
e.printStackTrace(logger);
logger.println("Could not initialize SSL context");
LOGGER.error("Could not initialize SSL context", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.kohsuke.stapler.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.net.ssl.SSLContext;
Expand Down Expand Up @@ -99,6 +101,8 @@
*/
public class StashNotifier extends Notifier implements SimpleBuildStep {

private static final Logger LOGGER = LoggerFactory.getLogger(StashNotifier.class);

public static final int MAX_FIELD_LENGTH = 255;
public static final int MAX_URL_FIELD_LENGTH = 450;

Expand Down Expand Up @@ -458,7 +462,7 @@ private boolean processJenkinsEvent(
} catch (Exception e) {
logger.println("Caught exception while notifying Bitbucket with id "
+ commitSha1);
e.printStackTrace(logger);
LOGGER.error("{} failed to notify Bitbucket for {}", idOf(run), commitSha1, e);
}
}
if (commitSha1s.isEmpty()) {
Expand All @@ -483,7 +487,7 @@ protected Collection<String> lookupCommitSha1s(
}
} catch (IOException | InterruptedException | MacroEvaluationException e) {
logger.println("Unable to expand commit SHA value");
e.printStackTrace(logger);
LOGGER.error("{} unable to expand commit SHA value", idOf(run), e);
return Collections.emptyList();
}
}
Expand Down Expand Up @@ -558,11 +562,11 @@ protected CloseableHttpClient getHttpClient(PrintStream logger, Run<?, ?> run, S
HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(registry);
clientBuilder.setConnectionManager(connectionManager);
} catch (NoSuchAlgorithmException nsae) {
logger.println("Couldn't establish SSL context:");
nsae.printStackTrace(logger);
logger.println("Could not establish SSL context");
LOGGER.error("{} could not establish SSL context", idOf(run), nsae);
} catch (KeyManagementException | KeyStoreException e) {
logger.println("Couldn't initialize SSL context:");
e.printStackTrace(logger);
logger.println("Could not initialize SSL context");
LOGGER.error("{} could not initialize SSL context", idOf(run), e);
}
}

Expand Down Expand Up @@ -975,8 +979,8 @@ private String expandStashURL(Run<?, ?> run, final TaskListener listener) {

} catch (IOException | InterruptedException | MacroEvaluationException ex) {
PrintStream logger = listener.getLogger();
logger.println("Unable to expand Bitbucker server URL");
ex.printStackTrace(logger);
logger.println("Unable to expand Bitbucket server URL");
LOGGER.error("{} unable to expand Bitbucket server URL", idOf(run), ex);
}
return url;
}
Expand Down Expand Up @@ -1085,7 +1089,7 @@ protected String getBuildKey(final Run<?, ?> run,
}
} catch (IOException | InterruptedException | MacroEvaluationException ioe) {
logger.println("Cannot expand build key from parameter. Processing with default build key");
ioe.printStackTrace(logger);
LOGGER.error("{} cannot expand build key from parameter - using default", idOf(run), ioe);
key.append(getDefaultBuildKey(run));
}
} else {
Expand All @@ -1095,6 +1099,10 @@ protected String getBuildKey(final Run<?, ?> run,
return StringEscapeUtils.escapeJavaScript(key.toString());
}

private static String idOf(Run<?, ?> run) {
return run != null ? run.getExternalizableId() : "(absent run)";
}

/**
* Returns the build name to be pushed. This will select the specifically overwritten build name
* or get the build name from the {@link Run}.
Expand Down

0 comments on commit c720b95

Please sign in to comment.