Skip to content

Instantly share code, notes, and snippets.

@a3linux
Last active March 24, 2022 06:50
Show Gist options
  • Save a3linux/a786d929815a7fc43f22acb00e8024ff to your computer and use it in GitHub Desktop.
Save a3linux/a786d929815a7fc43f22acb00e8024ff to your computer and use it in GitHub Desktop.
A Groovy class for Pretty Log / Output from Jenkins pipeline
/**
* PrettyLog class used for pretty and clean log
*/
class PrettyLog implements Serializable {
def steps
def env
/**
* COnstructor
* @param steps Jenkins pipeline stpes
* @param env Jenkins pipeline env variables
*/
PrettyLog(steps, env) {
this.steps = steps
this.env = env
}
/** echoLine - Output single line log with prefix and suffix
* @param msg The message
*/
void echoLine(String msg) {
String xuffix = "---------------------"
if (msg instanceof String) {
steps.echo xuffix + msg + xuffix
} else {
steps.echo "Please pass a String"
}
}
/**
* echoBlock - output a block/banner log
* @param msgs multiple string messages are supported
*/
void echoBlock(java.lang.String... msgs) {
steps.echo createBlock(msgs)
}
/**
* errorBlock - Error signal with a block log
* @param msgs multiple string messages are supported
*/
void errorBlock(java.lang.String... msgs) {
steps.error(createBlock(msgs))
}
/**
* createBlock - create the log block
* @param msgs multiple string messages are supported
*/
String createBlock(java.lang.String... msgs) {
return """
*****************************************************
${msgFlatten(null, msgs).join("\n")}
*****************************************************
"""
}
/**
* msgFlatten - method to flattern String(s)
* @param list List as input/output
* @param msgs The String(s)/Messages
*/
List msgFlatten(list, msgs) {
list = list ?: []
if (!(msgs instanceof String) && !(msgs instanceof GString)) {
msgs.each { msg -> list = msgFlatten(list, msg) }
} else {
list += msgs.stripIndent().stripMargin().trim()
}
return list
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment