微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我使用以下代码挂接到SBT的日志记录系统中,以将记录消息发送到可通过服务器设置访问的另一个进程:

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)
}

当我看到其他服务器进程中输出输出显示时,我看不到绿色[成功]标签出现的消息.其他一切(即所有的[信息]信息和红色[错误]信息)都显得很好.

打印clientLogger.successEnabled让我知道.

我究竟做错了什么?

解决方法

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

在咨询the sources of sbt之后,我的理解是,extraLoggers是一个只有“A function that provides additional loggers for a given setting.”的设置,这些额外的记录器除了StandardMain.console之外.

如果可能的话,您必须设置logManager以引用extraLogger和您自己的build.sbt中的自定义sbt.ConsoleOut,例如.

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()
  }
})

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

话虽如此,我相信,在sbt 0.13.1中,当showSuccess为true时,它不可能“拦截”打印输出的[success]消息,因为从控制台外面的ConsoleOut出来.

您可以使用extraLogger做什么是拥有自己的任务自定义日志记录,而streams.value.log.success(“Succezz”)应该可以工作.

示例build.sbt与extraLoggers和t任务演示自定义记录器.

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")
}

使用该文件,执行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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐