Jenkins DSL, Get "stage logs" as list and print the failures

I am working with Jenkis [DSL].
Do you know a Jenkins API to get all the logs of the JOB?

For example, there is a method to get console output:

def x = currentBuild.getRawBuild().getLog()

If there is an API, is it possible to convert the stage logs to a list and print the error log where the stage failed (red color)?

I am trying to print the relevant error and not all console outputs like

def x = currentBuild.getRawBuild().getLog()
println (x)

Desired process:


Hello @oded and welcome to this community :wave:

I may be wrong, but I think you can use the getLogText() method to retrieve the entire console log as a string.

To extract the logs for a particular stage, you could maybe use the Jenkins Pipeline step([$class: 'LogParserStep']) step. This step is used to parse and extract data from log files generated during a build.

Here is an example of how you could extract the error logs from the console output of a specific stage:

def logParser = [:]
stage('Build') {
    try {
        // Build stage logic here
    } catch (e) {
        logParser = step([$class: 'LogParserStep', 
            parsingRulesPath: 'path/to/parsing/rules'])
        // parsingRulesPath is optional if you have defined rules inline
        // use the parsed data to extract the relevant error logs
        def errorLogs = logParser["errors"]
        throw e

In the above example, the LogParserStep step is used to extract data from the console log generated during the build stage. The parsed data is then used to extract the error logs and print them to the console.

You can define your own parsing rules to extract the relevant error logs. The parsingRulesPath parameter can be used to specify the path to the parsing rules file. If the parsing rules are defined inline, you can omit the parsingRulesPath parameter.