From 169e8f81925805770e833f387756acdc5b02500c Mon Sep 17 00:00:00 2001 From: Naresh Rayapati Date: Wed, 2 Jan 2019 16:52:06 -0600 Subject: [PATCH] JENKINS-55194 Add logLevel to support debugging and also reduce default excessive logging (#12) * JENKINS-55194 Add logLevel to support debugging and also reduce default excessive logging * Address pull request comments * Address pull request comments --- CHANGELOG.adoc | 3 +++ README.adoc | 19 ++++++++++++++++- .../plugins/sshsteps/SSHService.groovy | 21 ++++++++++++++++--- .../plugins/sshsteps/util/Common.groovy | 18 ++++++++++++++++ .../plugins/sshsteps/steps/ScriptStep.java | 2 -- 5 files changed, 57 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 887effa..b696f28 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -2,6 +2,9 @@ == 1.2.0 (Unreleased) +* https://issues.jenkins-ci.org/browse/JENKINS-55194[JENKINS-55194] - Add logLevel to support debugging and also reduce default excessive logging. +* Code cleanup, update documentation. + == 1.1.1 * https://issues.jenkins-ci.org/browse/JENKINS-53556[JENKINS-53556] - Fix invalid proxy settings and validation. diff --git a/README.adoc b/README.adoc index 89fe677..55412a7 100644 --- a/README.adoc +++ b/README.adoc @@ -97,7 +97,6 @@ Most of the steps in this plugin require a common step variable called `remote`, |String |Encoding of input and output on the command or shell execution. Defaults to `UTF-8`. - |proxy |Proxy, refer below. |If this is set, the proxy server is used to reach the remote host. Defaults to no proxy. @@ -105,6 +104,24 @@ Most of the steps in this plugin require a common step variable called `remote`, |gateway |Remote |Gateway remote host. If this is set, the port-forwarding tunnel is used to reach the remote host. Defaults to no gateway. + +|appendName +|boolean +|If this is `true`, `name` is prefixed to each line in the log output. New format: `name\|log`. + +|logLevel +|String +a|Defaults to *SEVERE* + +Possible values, refer to java logging https://docs.oracle.com/javase/7/docs/api/java/util/logging/Level.html[levels] + +* SEVERE (highest value) +* WARNING +* INFO +* CONFIG +* FINE +* FINER +* FINEST (lowest value) |=== === Proxy diff --git a/src/main/groovy/org/jenkinsci/plugins/sshsteps/SSHService.groovy b/src/main/groovy/org/jenkinsci/plugins/sshsteps/SSHService.groovy index 408f4a3..b9ce158 100644 --- a/src/main/groovy/org/jenkinsci/plugins/sshsteps/SSHService.groovy +++ b/src/main/groovy/org/jenkinsci/plugins/sshsteps/SSHService.groovy @@ -10,6 +10,7 @@ import org.jenkinsci.plugins.sshsteps.util.Common import org.jenkinsci.plugins.sshsteps.util.CustomLogHandler import org.slf4j.MDC +import java.util.logging.Level import java.util.logging.Logger /** @@ -52,7 +53,13 @@ class SSHService implements Serializable { * Register Log handler for all hidetake's classes. */ private void registerLogHandler() { - Logger.getLogger("org.hidetake").addHandler(new CustomLogHandler(logger, MDC.get("execution.id"))) + Logger rootLogger = Logger.getLogger("org.hidetake") + rootLogger.addHandler(new CustomLogHandler(logger, MDC.get("execution.id"))) + if(remote.logLevel) { + rootLogger.setLevel(Level.parse(remote.logLevel)) + } else { + rootLogger.setLevel(Level.SEVERE) + } } private void validateRemote() { @@ -98,15 +105,18 @@ class SSHService implements Serializable { // Avoid excessive logging in Jenkins master. logging = LoggingMethod.none + def logPrefix = remote.appendName ? "$remote.name|" : '' + // Pipe logs to TaskListener's print stream. interaction = { when(line: _, from: standardOutput) { - logger.println("$remote.name|$it") + logger.println("$logPrefix$it") } when(line: _, from: standardError) { - logger.println("$remote.name|$it") + logger.println("$logPrefix$it") } } + if (remote.pty) { pty = remote.pty } @@ -161,6 +171,7 @@ class SSHService implements Serializable { * @return response from ssh run. */ def executeCommand(String command, boolean sudo) { + logger.println("Executing command on $remote.name[$remote.host]: $command sudo: $sudo") registerLogHandler() defineRemote(remote) ssh.run { @@ -180,6 +191,7 @@ class SSHService implements Serializable { * @return response from ssh run. */ def executeScriptFromFile(String pathname) { + logger.println("Executing script on $remote.name[$remote.host]: $pathname") registerLogHandler() defineRemote(remote) ssh.run { @@ -197,6 +209,7 @@ class SSHService implements Serializable { * @return response from ssh run. */ def put(String from, String into) { + logger.println("Sending a file/directory to $remote.name[$remote.host]: from: $from into: $into") registerLogHandler() defineRemote(remote) ssh.run { @@ -214,6 +227,7 @@ class SSHService implements Serializable { * @return response from ssh run. */ def get(String from, String into) { + logger.println("Receiving a file/directory from $remote.name[$remote.host]: from: $from into: $into") registerLogHandler() defineRemote(remote) ssh.run { @@ -230,6 +244,7 @@ class SSHService implements Serializable { * @return output from ssh's remove operation. */ def remove(String path) { + logger.println("Removing a file/directory on $remote.name[$remote.host]: $path") registerLogHandler() defineRemote(remote) ssh.run { diff --git a/src/main/groovy/org/jenkinsci/plugins/sshsteps/util/Common.groovy b/src/main/groovy/org/jenkinsci/plugins/sshsteps/util/Common.groovy index 45417c5..763dbfe 100644 --- a/src/main/groovy/org/jenkinsci/plugins/sshsteps/util/Common.groovy +++ b/src/main/groovy/org/jenkinsci/plugins/sshsteps/util/Common.groovy @@ -3,6 +3,8 @@ package org.jenkinsci.plugins.sshsteps.util import edu.umd.cs.findbugs.annotations.SuppressFBWarnings import org.hidetake.groovy.ssh.core.ProxyType +import java.util.logging.Level + /** * Basic validation for remote. * @@ -39,6 +41,9 @@ class Common { validateUserAuthentication(remote) validateHostAuthentication(remote) validateProxyConnection(remote) + if (remote.logLevel) { + validateLogLevel(remote) + } if (remote.gateway) { validateRemote(remote.gateway) } @@ -91,4 +96,17 @@ class Common { } } } + + /** + * Validate log level. + * + * @param remote map of values. + */ + private void validateLogLevel(remote) { + try { + Level.parse(remote.logLevel) + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException(getPrefix() + "Bad log level $remote.logLevel for $remote.name") + } + } } diff --git a/src/main/java/org/jenkinsci/plugins/sshsteps/steps/ScriptStep.java b/src/main/java/org/jenkinsci/plugins/sshsteps/steps/ScriptStep.java index 63b9b24..b9a0896 100644 --- a/src/main/java/org/jenkinsci/plugins/sshsteps/steps/ScriptStep.java +++ b/src/main/java/org/jenkinsci/plugins/sshsteps/steps/ScriptStep.java @@ -78,8 +78,6 @@ protected Object run() throws Exception { throw new IllegalArgumentException(path.getRemote() + " is a directory."); } - getListener().getLogger().println("Started Script from workspace: " + step.getScript()); - return getLauncher().getChannel() .call(new ScriptCallable(step, getListener(), path.getRemote())); }