如何为流任务的日志成员设置SBT logLevel?

时间:2019-01-03 17:46:52

标签: scala logging sbt task log-level

您如何设置logLevel when using streams.value.log in an SBT task

例如,如何将此任务的日志替换为Warn日志?:

lazy val mainRunner = taskKey[Seq[File]]("Runs a main method")
lazy val mainRunnerSetting = mainRunner := {


  val logger = streams.value.log

  (runner in Compile).value.run(
    mainClass = "com.me.Main",
    classpath = (dependencyClasspath in Compile).value.files,
    log = logger,
    options = Array()
  )
}

在任务的内部和外部,我尝试了各种logLevel设置,例如:

logLevel in run in Compile := Level.Warn
logLevel := Level.Warn
logLevel in mainRunner := Level.Warn
logLevel in streams := Level.Warn

...但是执行此任务时,它始终设置为Info

1 个答案:

答案 0 :(得分:1)

不确定您的问题出在哪里,但是此build.sbt内容可以通过设置任务的日志级别来正常工作

val mainRunner = taskKey[Unit]("Print some log")
val runner2 = taskKey[DummyRun]("hold dummy runner")

lazy val root = project
  .in(file("."))
  .settings(
    runner2 := new DummyRun,
    mainRunner := {
      val logger = streams.value.log
      logger.warn("Hello warn")
      logger.info("Hello info")
      logger.debug("Hello debug")
      runner2.value.run(logger)
    },
    (logLevel in mainRunner) := Level.Warn
  )

DummyRunproject文件夹中包含内容的类

import sbt.Logger

class DummyRun {
  def run(log: Logger): Unit = {
    log.warn("DummyRun warn")
    log.info("DummyRun info")
    log.debug("DummyRun debug")
  }
}

输出:

> mainRunner
[warn] Hello warn
[warn] DummyRun warn