How to output slf4j log messages from sbt?

Viewed 389

I'm currently writing an sbt plugin. The tasks that I define are using functionality from libraries that are otherwise unrelated to sbt and that I don't want to change. These libraries use slf4j for logging. I would like the logging output to show up in the sbt console as if I had used streams.value.log, so that I can e. g. turn debug logging on and off in the usual sbt ways like set someTask / logLevel := Level.Debug. Is there a way to do this?

It seems like sbt-slf4j is supposed to solve my problem:

https://index.scala-lang.org/eltimn/sbt-slf4j/sbt-slf4j/1.0.4?target=_2.12

But I wasn't able to get it to work. My project/build.sbt looks like this:

libraryDependencies += "com.eltimn" %% "sbt-slf4j" % "1.0.4"
resolvers += Resolver.bintrayRepo("eltimn", "maven")

and build.sbt like so:

import org.slf4j.LoggerFactory
import org.slf4j.impl.SbtStaticLoggerBinder

val foo = taskKey[Unit]("foobar")

foo := {
  SbtStaticLoggerBinder.sbtLogger = streams.value.log

  // symbolic implementation, the actual implementation lives in a library
  val logger = LoggerFactory.getLogger("foobar")
  logger.warn("Hello!")
}

But running foo does not result in a warning being printed. A warning is printed if I change it to LoggerFactory.getLogger(streams.value.log.name), but this is not an option because again, the code lives in a library.

Is there any good way to solve this?

0 Answers
Related