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

Refactor logging to not need "this" in constructor #194

Merged
merged 4 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ java {
withSourcesJar()
}

task removeDuplicateFiles(type: Delete) {
delete "${buildDir}/tmp/groovydoc"
}

groovydoc {
use = true
overviewText = resources.text.fromString('Bringing the Zen of Python to Jenkins.')
Expand All @@ -43,6 +47,8 @@ task groovydocJar(type: Jar, dependsOn: groovydoc ) {
from groovydoc.destinationDir
}

groovydoc.finalizedBy removeDuplicateFiles

publishing {
publications {
mavenJava(MavenPublication) {
Expand Down
2 changes: 1 addition & 1 deletion jobs/logging/logging_example.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.dsty.logging.LogClient
node() {
String cps = sh(script: '#!/bin/bash\nset +x; > /dev/null 2>&1\necho Test for CPS issue', returnStdout: true)

LogClient log = new LogClient(this)
LogClient log = new LogClient()

String level = 'default'

Expand Down
2 changes: 1 addition & 1 deletion src/org/dsty/bash/BashClient.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class BashClient implements Serializable {
*/
BashClient(Object steps) {
this.steps = steps
this.log = new LogClient(steps)
this.log = new LogClient()
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/org/dsty/github/actions/Action.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Action implements Serializable {
*/
Action(Object steps) {
this.steps = steps
this.log = new LogClient(steps)
this.log = new LogClient()
this.bash = new BashClient(steps)
}

Expand Down
2 changes: 1 addition & 1 deletion src/org/dsty/github/actions/ActionFactory.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ActionFactory implements Serializable {
*/
ActionFactory(Object steps) {
this.steps = steps
this.log = new LogClient(steps)
this.log = new LogClient()
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/org/dsty/github/actions/Step.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Step implements Serializable {
*/
Step(Object steps) {
this.steps = steps
this.log = new LogClient(steps)
this.log = new LogClient()
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/org/dsty/github/actions/Workflow.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class Workflow implements Serializable {
*/
Workflow(Object steps) {
this.steps = steps
this.log = new LogClient(steps)
this.log = new LogClient()
this.bash = new Bash()
}

Expand Down
51 changes: 42 additions & 9 deletions src/org/dsty/logging/LogClient.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,62 @@ package org.dsty.logging
import static groovy.json.JsonOutput.prettyPrint
import static groovy.json.JsonOutput.toJson
import static org.dsty.jenkins.Instance.pluginInstalled
import org.dsty.jenkins.Build

import com.cloudbees.groovy.cps.NonCPS

/**
* Basic logger that uses the <a href="https://plugins.jenkins.io/ansicolor/">AnsiColor</a>
* plugin to output log messages in color.
* <p>
* Set the environment variable {@code PIPELINE_LOG_LEVEL} to
* {@code DEBUG}, {@code INFO}, {@code WARN} or {@code ERROR} to
* Set the environment variable <code>PIPELINE_LOG_LEVEL</code> to
* <code>DEBUG</code>, <code>INFO</code>, <code>WARN</code> or <code>ERROR</code> to
* control logger output. Setting it to any other value will stop
* all output. If unset it defaults to {@code INFO}.
* all output. If unset it defaults to <code>INFO</code>.
*/
class LogClient implements Serializable {

/**
* Workflow script representing the jenkins build.
*/
private final Object steps
private Object steps

/**
* If we should print in color.
*/
Boolean printColor
final private Boolean printColor

/**
* Default Constructor
* <p>
* Example:
* <pre>{@code
* import org.dsty.logging.LogClient
*LogClient log = new LogClient()
* }</pre>
*
* @since 0.12.0
*/
LogClient() {
this.printColor = useColor()
}

/**
* Optional Constructor for backwards compatability
* <p>
* Example:
* <pre>{@code
* import org.dsty.logging.LogClient
*LogClient log = new LogClient(this)
* }</pre>
*
* @deprecated As of release 0.12.0, replaced by {@link #LogClient()}
* @param steps The workflow script representing the jenkins build.
*/
@Deprecated
LogClient(Object steps) {
this.steps = steps
useColor()
this.printColor = useColor()
}

/**
Expand Down Expand Up @@ -110,7 +129,21 @@ class LogClient implements Serializable {
return prettyPrint(toJson(item))
}

String writeMsg(Object input, String colorCode) {
/**
* Load the workflow steps from the current Build.
*/
private Object getSteps() {

if (!this.@steps) {
final Build currentBuild = new Build()
/* groovylint-disable-next-line UnnecessaryGetter */
this.steps = currentBuild.getWorkFlowScript()
}

return this.@steps
}

private String writeMsg(Object input, String colorCode) {
if (this.printColor) {
this.steps.ansiColor('xterm') {
this.steps.println("\u001B[${colorCode}m${input}\u001B[0m")
Expand Down Expand Up @@ -141,8 +174,8 @@ class LogClient implements Serializable {
}

@NonCPS
private void useColor() {
this.printColor = pluginInstalled('ansicolor')
private Boolean useColor() {
return pluginInstalled('ansicolor')
}

}
2 changes: 1 addition & 1 deletion src/org/dsty/scm/Generic.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Generic implements Serializable {
*/
Generic(Object steps) {
this.steps = steps
this.log = new LogClient(steps)
this.log = new LogClient()
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/org/dsty/system/os/shell/Bash.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class Bash implements Shell {
}

private void setLog() {
this.log = new LogClient(this.wfs)
this.log = new LogClient()
}

}