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

How to set SBT logLevel for log member of a streams task?

如何设置logLevelwhen 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

不确定您的问题出在哪里,但是这个 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 文件夹中的 class,内容为

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