Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom extraLogger not getting [success] messages in sbt?

Tags:

scala

sbt

I'm using the following code to hook into SBT's logging system to send the logging messages to another process accessible via a server setting:

extraLoggers := {
  val clientLogger = FullLogger {
    new Logger {
      def log(level: Level.Value, message: => String): Unit =
        if(level >= Level.Info) server.value.send(Json.arr("print", level.toString(), message))
      def success(message: => String): Unit = server.value.send(Json.arr("print", "info", message))
      def trace(t: => Throwable): Unit = server.value.send(Json.arr("print", "error", t.toString))
    }
  }
  val currentFunction = extraLoggers.value
  (key: ScopedKey[_]) => clientLogger +: currentFunction(key)
}

When I look at the output being spewed out on the other server process, I don't see the messages with green [success] tags appearing. Everything else (i.e. all the [info] messages and the red [error] messages) appear just fine.

Printing out clientLogger.successEnabled gives me true.

What am I doing wrong?

like image 460
Li Haoyi Avatar asked Nov 18 '13 07:11

Li Haoyi


1 Answers

DISCLAIMER: Please use with care as the answer might be incomplete or even totally wrong.

After consulting the sources of sbt my understanding is that extraLoggers is a setting that is only "A function that provides additional loggers for a given setting." and these additional loggers are additional to StandardMain.console.

If it were possible, you would therefore have to set logManager to have a reference to extraLoggers and your own custom sbt.ConsoleOut in build.sbt, e.g.

logManager := LogManager.defaults(extraLoggers.value, new ConsoleOut {
  val lockObject = System.out
  def print(s: String): Unit = synchronized { print(s) }
  def println(s: String): Unit = synchronized { println(s) }
  def println(): Unit = synchronized {
    System.out.println()
  }
})

It won't however work since sbt.ConsoleOut is a sealed trait and hence there is no way to use it outside the file it was defined.

Having said that, I believe, in sbt 0.13.1, it's not possible to "intercept" the [success] message that's printed out when showSuccess is true as it comes out from ConsoleOut that's outside your control.

What you can do with extraLoggers is to have your own custom logging for tasks and streams.value.log.success("Succezz") should work.

The sample build.sbt with extraLoggers and a t task to demo the custom logger.

extraLoggers := {
  val clientLogger = FullLogger {
    new Logger {
      def log(level: Level.Value, message: => String): Unit =
        if(level >= Level.Info) println(s"+++ $message at $level")
      def success(message: => String): Unit = println(s"+++ success: $message")
      def trace(t: => Throwable): Unit = println(s"+++ trace: throwable: $t")
    }
  }
  val currentFunction = extraLoggers.value
  (key: ScopedKey[_]) => clientLogger +: currentFunction(key)
}

val t = taskKey[Unit]("Show extraLoggers")

t := {
  println(s"Using extraLoggers")
  val s: TaskStreams = streams.value
  val log = s.log
  log.debug("Saying hi...")
  log.info("Hello!")
  log.error("Error")
  log.success("Succezz")
}

With the file, executing the t task gives the following output:

$ sbt
[info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
[info] Loading project definition from /Users/jacek/sandbox/sbt-0.13.1-extra-loggers/project
[info] Set current project to sbt-0-13-1-extra-loggers (in build file:/Users/jacek/sandbox/sbt-0.13.1-extra-loggers/)
[sbt-0-13-1-extra-loggers]> about
[info] This is sbt 0.13.1
[info] The current project is {file:/Users/jacek/sandbox/sbt-0.13.1-extra-loggers/}sbt-0-13-1-extra-loggers 0.1-SNAPSHOT
[info] The current project is built against Scala 2.10.3
[info] Available Plugins: com.typesafe.sbt.SbtGit, com.typesafe.sbt.SbtProguard, growl.GrowlingTests, org.sbtidea.SbtIdeaPlugin, np.Plugin, com.timushev.sbt.updates.UpdatesPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.3
[sbt-0-13-1-extra-loggers]> t
Using extraLoggers
[info] Hello!
+++ Hello! at info
[error] Error
+++ Error at error
[success] Succezz
+++ success: Succezz
[success] Total time: 0 s, completed Dec 16, 2013 10:30:48 PM
like image 124
Jacek Laskowski Avatar answered Oct 28 '22 11:10

Jacek Laskowski