2

在 SBT 任务中logLevel 使用时streams.value.log如何设置?

例如,如何将此任务的日志压缩为仅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这个任务执行的时间

4

1 回答 1

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
  )

包含内容的文件夹DummyRun中的类在哪里project

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
于 2019-01-05T09:51:16.077 回答