您如何设置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
答案 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
)
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