Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging syntax for Play Framework 2 in Scala

This is a really silly question, but how can you do convenient formatting of log strings in Play Framework 2 (and in Scala?).

I've googled but its very difficult to find an example, essentially most links are talking about configuring Logback in the first place which I've done fine.

I'm basically trying to find the best stylistic way to do something like:

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

Coming from a C# background (and log4net) I'd assume you could do something like:

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

But I can't see how this would work with the trait the way it is defined. I've also seen vague references to how you might be able to avoid checking Logger.isDebugEnabled by using a lazy evaluative syntax like:

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

That uses Scala macros - but again, that doesn't work and I can find very little information about it.

Am I missing something really blatant here?

like image 969
Kieran Benton Avatar asked Sep 21 '13 11:09

Kieran Benton


People also ask

Does Play framework use Log4j?

Logging configurationThe Play logger is built on Log4j. Since most Java libraries use Log4j or a wrapper able to use Log4j as a backend, you can easily configure logging that is well-suited to your application.

Is Play framework asynchronous?

Internally, Play Framework is asynchronous from the bottom up. Play handles every request in an asynchronous, non-blocking way. The default configuration is tuned for asynchronous controllers.

What is activator in play framework?

The activator command can be used to create a new Play application. Activator allows you to select a template that your new application should be based off. For vanilla Play projects, the names of these templates are play-scala for Scala based Play applications, and play-java for Java based Play applications.


1 Answers

  1. The framework used for logging is logback. When you type : Logger.debug, the isDebugEnabled is already implicitly checked.

  2. For the syntax of logging, use the Scala string interpolation.

    Logger.debug(s"Modified: Id = '$real_session_id', Modified = $modified.toString")
    
like image 185
i.am.michiel Avatar answered Sep 28 '22 12:09

i.am.michiel