如何使用debug / warning / error从命令和控制日志级别进行日志记录?

时间:2015-02-05 11:38:23

标签: sbt

在阅读SBT中的documentation about logging后,我知道如何通过streams.value.log从任务中记录。我做得很广泛。但是从自定义命令记录呢?

在一个命令中,我们可以使用state,而记录的唯一有趣的事情是globalLogging,但我不知道如何使用它。

3 个答案:

答案 0 :(得分:2)

State类有一个log成员,通过一些隐含的魔法添加,你可以使用。如果没有隐含的魔法,您可以使用显式globalLogging成员来访问full记录器。

示例:

def hello = Command.command("hello") { state =>
  // These are equivalent
  state.log.info("Hello!")
  state.globalLogging.full.info("Hello!")

  state
}

答案 1 :(得分:1)

tl; dr 我主要猜测,但评论部分也不合适。

sbt 0.13.8-M2

我在hello中使用以下build.sbt命令来streams.value.log

val hello: Command = Command.command("hello") { (s: State) =>
    val extracted: Extracted = Project.extract(s)
    import extracted._

    // streams.value.log
    val Value(v) = Project.evaluateTask(streams in currentRef, s).get
    println(v.log)
    s
}

commands += hello

然而,v被初始化它似乎为时已晚,或者它不是用于命令,因此是下面的堆栈跟踪:

[sbt-learning-space]> hello
java.lang.RuntimeException: Streams for '{file:/Users/jacek/dev/sandbox/sbt-learning-space/}sbt-learning-space/*:streams' have been closed.
    at scala.sys.package$.error(package.scala:27)
    at sbt.std.Streams$$anon$3$$anon$2.checkOpen(Streams.scala:146)
    at sbt.std.Streams$$anon$3$$anon$2.make(Streams.scala:127)
    at sbt.std.Streams$$anon$3$$anon$2.text(Streams.scala:113)
    at sbt.std.Streams$$anon$3$$anon$2.log(Streams.scala:124)
    at $129b92cf2e7230691a78$$anonfun$1.apply(build.sbt:71)
    at $129b92cf2e7230691a78$$anonfun$1.apply(build.sbt:64)
    at sbt.Command$$anonfun$command$1$$anonfun$apply$1.apply(Command.scala:29)
    at sbt.Command$$anonfun$command$1$$anonfun$apply$1.apply(Command.scala:29)
    at sbt.Command$.process(Command.scala:92)
    at sbt.MainLoop$$anonfun$1$$anonfun$apply$1.apply(MainLoop.scala:98)
    at sbt.MainLoop$$anonfun$1$$anonfun$apply$1.apply(MainLoop.scala:98)
    at sbt.State$$anon$1.process(State.scala:184)
    at sbt.MainLoop$$anonfun$1.apply(MainLoop.scala:98)
    at sbt.MainLoop$$anonfun$1.apply(MainLoop.scala:98)
    at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:17)
    at sbt.MainLoop$.next(MainLoop.scala:98)
    at sbt.MainLoop$.run(MainLoop.scala:91)
    at sbt.MainLoop$$anonfun$runWithNewLog$1.apply(MainLoop.scala:70)
    at sbt.MainLoop$$anonfun$runWithNewLog$1.apply(MainLoop.scala:65)
    at sbt.Using.apply(Using.scala:24)
    at sbt.MainLoop$.runWithNewLog(MainLoop.scala:65)
    at sbt.MainLoop$.runAndClearLast(MainLoop.scala:48)
    at sbt.MainLoop$.runLoggedLoop(MainLoop.scala:32)
    at sbt.MainLoop$.runLogged(MainLoop.scala:24)
    at sbt.StandardMain$.runManaged(Main.scala:53)
    at sbt.xMain.run(Main.scala:28)
    at xsbt.boot.Launch$$anonfun$run$1.apply(Launch.scala:109)
    at xsbt.boot.Launch$.withContextLoader(Launch.scala:128)
    at xsbt.boot.Launch$.run(Launch.scala:109)
    at xsbt.boot.Launch$$anonfun$apply$1.apply(Launch.scala:35)
    at xsbt.boot.Launch$.launch(Launch.scala:117)
    at xsbt.boot.Launch$.apply(Launch.scala:18)
    at xsbt.boot.Boot$.runImpl(Boot.scala:41)
    at xsbt.boot.Boot$.main(Boot.scala:17)
    at xsbt.boot.Boot.main(Boot.scala)
[error] Streams for '{file:/Users/jacek/dev/sandbox/sbt-learning-space/}sbt-learning-space/*:streams' have been closed.
[error] Use 'last' for the full log.

我的理解是,命令不在streams.value.log的范围内,因为他们要为state配置设置。

答案 2 :(得分:0)

一种方法是使用println(),但您无法控制日志级别(debugwarningerror)。