Logging syntax for Play Framework 2 in Scala

This is a really stupid question, but how can you conveniently format log lines in Play Framework 2 (and in Scala?).

I searched googled, but it is very difficult to find an example, in fact, most of the links talk about setting up Logback, in the first place, which I did well.

Basically I am trying to find a better stylistic way to do something like:

if(Logger.isDebugEnabled) Logger.debug("Modified: Id = '" + real_session_id + "', Modified = " + modified.toString) 

Based on the background of C # (and log4net), I would suggest that you can do something like:

 if(Logger.isDebugEnabled) Logger.debug("Modified: Id = '{0}', Modified = {1}", real_session_id, modified.toString) 

But I donโ€™t see how this will work with a sign as it is defined. I also saw vague references to how you could avoid checking Logger.isDebugEnabled using lazy evaluative syntax, for example:

 Logger.debug("Modified: Id = ${real_session_id}, Modified = ${modified.toString}") 

It uses Scala macros, but again, it does not work, and I can find very little information about it.

Did I miss something really egregious here?

+7
scala logging playframework
source share
2 answers
  • The frame used for logging is logback. When you enter: Logger.debug , isDebugEnabled already implicitly checked.

  • For logging syntax, use Scala string interpolation.

     Logger.debug(s"Modified: Id = '$real_session_id', Modified = $modified.toString") 
+9
source share

Why not just use the standard language / stdlib String interpolation features? http://docs.scala-lang.org/overviews/core/string-interpolation.html

I apologize if I missed something important in your question.

To avoid the if (Logger.isDebugEnabled) check if (Logger.isDebugEnabled) structure does not provide some lazy evaluation scheme for the arguments passed to it, I would first consider defining my own wrappers:

 object MyLazyLogger { def debug(msg: => Any) = if (Logger.isDebugEnabled) Logger.debug(msg) } 

Also, I don't think that the way you interpolate stuff into a string has anything to do with not evaluating debug() arguments if logging is disabled - if debug() declares that it eagerly evaluates any arguments I donโ€™t see that you can switch to a lazy evaluation on the call site, simply using the โ€œspecial formโ€ of line interpolation. (I would be glad if someone proved to me that I was wrong here and taught me something new :))

Disclosure: I am not familiar with Play (yet), so I just take a picture with a common approach.

+1
source share

All Articles