2013-06-06 2 views
8

SLF4J의 가변 인자는 플레이에 2.1 프레임 워크/스칼라을이 간단한 예제를 수행하려고 내 자바 작업Play2.1 프레임 워크에서 SLF4J varargs를 어떻게 활용합니까? 로깅 호출에

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

에 매우 유용하며 나는 나를 거부하는 컴파일러로 실행합니다.

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를 호출 할 수 있습니까? 내가 할 수 없다면, 어떻게 로깅 호출에서 varargs를 얻기 위해 Play 2.1 로깅 프레임 워크를 사용할 수 있습니까? 스칼라 랜드에서 뭔가 이상한 것이 있습니다.

답변

6

SLF4J의 어떤 버전을 사용하고 있습니까? 1.6.6 이상으로 돌아갈 수 있다면이 문제를 모호하게 피할 수 있습니다. 이 두 서명은 불행히도 스칼라와 똑같은 것으로 보이며 컴파일러는 어떤 것을 의미하는지 구분할 수 없습니다. 일반적인 제안은이 오버로드 된 메소드 모호성이 존재하지 않는 SLF4J (가능한 경우) 버전으로 롤백하는 것입니다. 더 많은 정보는 아래 링크에서 확인할 수 있습니다

그냥 할 모든 것을 입력 할 마지막 인수를 강제로 다음과 같이

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

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

+0

위 답변입니다. 롤백은 나를위한 옵션이 아닙니다. 록 : 하드 플레이스. SLF4J의 API를 포기하고 Scala의 String.format에서 내 가변성을 가져야하는 것처럼 보입니다. 나는 SLF4J의 "{}"과 Java의 String.format의 벤치 마크를 수행했으며 SL4J가 위에 나왔습니다. 너무 나쁜 나는 가장 낮은 성능의 경로에 붙어있는 것 같습니다. 어쩌면 벤치 마크를 다시 실행하여 실제로 그렇게 나쁠 지 지켜 볼 시간입니다. –

1

이의 "빠른 수정이"

logger.debug("hello {}/{} ", "Hello", "World":Any) 

또는 귀하의 경우 (... 약간 적은 코드있게) 컴파일러의 문제 (들)을 해결합니다

log.info("Hello {}. The current time is {}", "robert", new java.util.Date():Any) 
관련 문제