如何在gradle buildSrc类中使用Logger?

时间:2015-03-19 22:19:39

标签: 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 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}")

我尝试了很多变体,比如在构造函数中设置类变量:

static private log = Logging.getLogger(MyClass)

此操作失败,在静态范围内找到了记录...

这里的魔力是什么?

1 个答案:

答案 0 :(得分:0)

Gradle User Guide, chapter 18表示您可以在buildSrc中使用SLF4j。我在课堂上对@ Slf4j注释很幸运。例如:

import groovy.util.logging.Slf4j

@Slf4j
class YourClass {
      def logIt(){
          log.info 'This is logged in Gradle'
      }
}