Tuesday, July 29, 2014

A minor annoyance with two argument logging in SLF4J and Scala

I am using Scala for one of my recent projects. For logging, I am using SLF4J/LOGback. There is one minor annoyance while you are trying to log two argument messages like:
logger.info("Some log with arg1 [{}] and arg2 [{}].", arg1, arg2)
While you compile with sbt you will get the following error:
[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,String)
 
If you are getting this error, a small trick that I did to avoid casting to AnyRef or Object was to just add a null argument at the end which will force the Scala compiler to make use of the vararg version. LOGBack just ignores extraneous arguments. Like this:
logger.info("Some log with arg1 [{}] and arg2 [{}].", arg1, arg2, null)

Disclaimer: I am a Scala rookie, hence take my advice with a pinch of salt!
 

No comments: