确切地说,我的Jenkins共享库中有以下代码:
文件名/vars/log.groovy
def call(msg, level = 'I') {
def levelString = '[INFO] '
switch (level) {
case 'D' :
levelString = '[DEBUG] '
break
case 'W' :
levelString = '[WARNING] '
break
case 'E' :
levelString = '[ERROR] '
break
}
echo levelString + msg
}
您可以看到它是用于记录的。现在,我以不同的步骤在每个地方使用此日志,但是我希望能够在Jenkinsfile
所以我将上面的代码更改为如下代码:
def logLevel = 'E'
public def setLogLevel(level) {
this.logLevel = level
}
def call(msg, level = 'I') {
def levelString = '[INFO] '
switch (level) {
case 'D' :
levelString = '[DEBUG] '
break
case 'W' :
levelString = '[WARNING] '
break
case 'E' :
levelString = '[ERROR] '
break
}
//just as an example without considering the hierarchy of levels
if(logLevel == level) {
echo levelString + msg
}
}
但是此代码不起作用。在jenkins中,错误表明它无法解析logLevel
,也不能在构建开始时调用setLogLevel(...)
,因为它也无法解析。