如何利用Play2.1框架中的SLF4J varargs日志记录?

时间:2013-06-06 17:57:17

标签: scala playframework

SLF4J关于日志记录调用的变量在我的Java工作中非常有用

Logger log = LoggerFactory.getLogger( getClass() );
log.debug( "Hello, {}.  The current time is {}", "robert", new Date() );

尝试在Play 2.1 Framework / Scala中执行此简单示例,我遇到编译器拒绝我。

import play.api._
import play.api.mvc._
import org.slf4j.LoggerFactory

object Application extends Controller {
  val log: org.slf4j.Logger = LoggerFactory.getLogger(getClass())

  def hb = Action {
    val message = makeMessage()
    // COMPILER HATES THIS:  ambiguous reference compiler error here
    log.info("Hello {}.  The current time is {}", "robert", new java.util.Date() )
    Ok(message)
  }
  def makeMessage(): String = { return "stuff" }
}

[dm2-server] $ compile
[info] Compiling 2 Scala sources to /Users/bobk/work/dm2-server/target/scala-2.10/classes...
[error] /Users/bobk/work/dm2-server/app/controllers/Application.scala:16: ambiguous reference to overloaded definition,
[error] both method info in trait Logger of type (x$1: String, x$2: <repeated...>[Object])Unit
[error] and  method info in trait Logger of type (x$1: String, x$2: Any, x$3: Any)Unit
[error] match argument types (String,String,java.util.Date)
[error]     log.info("Hello {}.  The current time is {}", "robert", new java.util.Date() )
[error]         ^
[error] one error found
[error] (compile:compile) Compilation failed
[error] Total time: 1 s, completed Jun 6, 2013 10:54:41 AM

该错误是什么?我如何克服它来调用SLF4J API?如果我不能这样做,我如何使用Play 2.1 Logging Framework来获取日志记录调用的变量? Scala-land中的东西不对。

1 个答案:

答案 0 :(得分:6)

您使用的是什么版本的SLF4J?如果你可以回到1.6.6或更高版本,你可以避免这个问题的含糊不清。不幸的是,这两个签名看起来与scala完全相同,编译器似乎无法区分你的意思。常见的建议是回滚到SLF4J的一个版本(如果你可能的话),这个重载的方法歧义将不存在。更多信息可以在以下链接中找到:

https://groups.google.com/forum/?fromgroups#!topic/scala-language/ms4IVIu-xGw

https://github.com/typesafehub/scalalogging/issues/16

相关问题