自定义extraLogger没有在sbt中获得[成功]消息?

Li *_*oyi 13 scala sbt

我正在使用以下代码挂钩到SBT的日志系统,以将日志消息发送到另一个可通过以下server设置访问的进程:

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)
}
Run Code Online (Sandbox Code Playgroud)

当我查看在另一个server进程中喷出的输出时,我看不到带有绿色[success]标记的消息.其他一切(即所有[info]消息和红色[error]消息)都显得很好.

打印出来clientLogger.successEnabled给了我true.

我究竟做错了什么?

Jac*_*ski 6

免责声明:请小心使用,因为答案可能不完整甚至完全错误.

在咨询了sbt的来源后,我的理解是这个extraLoggers设置只是"为给定设置提供额外记录器的功能".而这些additional loggers都是附加StandardMain.console.

如果有可能,你会因此必须设置日志管理到对基准extraLoggers和自己的自定义sbt.ConsoleOutbuild.sbt,如

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()
  }
})
Run Code Online (Sandbox Code Playgroud)

然而它不会起作用,因为它sbt.ConsoleOut是一个密封的特性,因此没有办法在它定义的文件之外使用它.

话虽如此,我相信,在sbt 0.13.1,这是不可能的"拦截"的[success]是集团的印刷出来的时候的消息showSuccesstrue,因为它出来的ConsoleOut那是你的控制范围之外.

你可以做的extraLoggers是拥有自己的自定义日志记录任务,streams.value.log.success("Succezz")应该工作.

build.sbt带有示例extraLoggerst自定义记录器演示任务的示例.

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")
}
Run Code Online (Sandbox Code Playgroud)

使用该文件,执行t任务将提供以下输出:

$ 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
Run Code Online (Sandbox Code Playgroud)