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"]
println(errorLogs)
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.