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

时间:2013-11-18 07:44:21

标签: 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)
}

当我查看另一个server进程中显示的输出时,我看不到带有绿色[success]标记的消息。其他所有内容(即所有[info]条消息和红色[error]消息)都显示正常。

打印clientLogger.successEnabled会给我true

我做错了什么?

1 个答案:

答案 0 :(得分:6)

免责声明:请谨慎使用,因为答案可能不完整甚至完全错误。

在咨询the sources of sbt之后,我的理解是extraLoggers设置仅为"A function that provides additional loggers for a given setting."且这些additional loggers 附加至{{3} }}

如果可能,那么您必须设置StandardMain.consoleextraLoggers引用build.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() } }) 中的自定义logManager },例如

sbt.ConsoleOut

它不会起作用,因为sbt 0.13.1是一个密封的特征,因此没有办法在它定义的文件之外使用它。

我相信,在[success]中,当showSuccess true出现时,我无法“拦截”打印出来的ConsoleOut消息来自extraLoggers,这是你无法控制的。

使用streams.value.log.success("Succezz")可以执行的操作是为任务创建自己的自定义日志记录,build.sbt应该有效。

示例extraLoggers textraLoggers := { 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") } 任务,以演示自定义记录器。

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 任务会得到以下输出:

{{1}}