Skip to content

Commit

Permalink
JENKINS-55194 Add logLevel to support debugging and also reduce defau…
Browse files Browse the repository at this point in the history
…lt excessive logging (#12)

* JENKINS-55194 Add logLevel to support debugging and also reduce default excessive logging
* Address pull request comments
* Address pull request comments
  • Loading branch information
nrayapati authored Jan 2, 2019
1 parent 8239206 commit 169e8f8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 18 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,31 @@ 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.

|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
Expand Down
21 changes: 18 additions & 3 deletions src/main/groovy/org/jenkinsci/plugins/sshsteps/SSHService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions src/main/groovy/org/jenkinsci/plugins/sshsteps/util/Common.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -39,6 +41,9 @@ class Common {
validateUserAuthentication(remote)
validateHostAuthentication(remote)
validateProxyConnection(remote)
if (remote.logLevel) {
validateLogLevel(remote)
}
if (remote.gateway) {
validateRemote(remote.gateway)
}
Expand Down Expand Up @@ -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")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down

0 comments on commit 169e8f8

Please sign in to comment.