如何在gradle buildSrc类中使用记录器?
所以,当我做一些幼稚的事情时,只要使用:如何在gradle buildSrc类中使用记录器?,gradle,Gradle,所以,当我做一些幼稚的事情时,只要使用: logger.info("something") 我得到: <myfile>.groovy: 52: Apparent variable 'logger' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes: You attempted to reference a variable
logger.info("something")
我得到:
<myfile>.groovy: 52: Apparent variable 'logger' was found in a
static scope but doesn't refer to a local variable, static field or class.
Possible causes:
You attempted to reference a variable in the binding or an
instance variable from a static context.
You misspelled a classname or statically imported field.
Please check the spelling.
You attempted to use a method 'logger' but left out brackets
in a place not allowed by the grammar.
@ line 52, column 9.
logger?.info("Resolving ${projectPath} to ${version}")
此操作失败,因为在静态范围中发现日志记录
这里的神奇之处是什么?说您可以在buildSrc中使用。我很幸运地在类中使用了@Slf4j注释。例如:
import groovy.util.logging.Slf4j
@Slf4j
class YourClass {
def logIt(){
log.info 'This is logged in Gradle'
}
}
gradle的日志记录似乎只在项目或任务上下文中才有意义。是的,绝对有意义。您需要获取
项目
实例才能使用记录器。因此,实现插件
或扩展默认任务
。
import groovy.util.logging.Slf4j
@Slf4j
class YourClass {
def logIt(){
log.info 'This is logged in Gradle'
}
}