Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force Logger.debug output in Play! framework specs2 tests?

By default all Logger output, visible when an application is running, is mute when the application is tested.

How to force the debugs, infos etc. to be shown in the specs2 reports?

like image 392
Rajish Avatar asked Jul 22 '12 01:07

Rajish


People also ask

How do I enable debugging in logger?

If it is specific to a certain application, then the application logger can be set to DEBUG. Steps: 1) Go to System Configuration -> Platform Configuration -> Logging . 2) Select Action -> Manage Appenders.

What is debug mode in logger?

debug . This combination of a configurable logging level and logging statements within your program allow you full control over how your application will log its activity.


1 Answers

First off, you may like some background why logging is disabled in test mode. This was Guillame Bort's answer to a question in the play forum (see this thread):

The logger is disabled in test mode for now because it was causing an huge PermGen space leak when running tests. But we are working to run tests in a forked JVM so we will enable it again soon.

As a workaround, I created my own logger like this (Scala code):

import play.api.{Play, LoggerLike, Logger} import org.slf4j.LoggerFactory import org.slf4j.impl.SimpleLoggerFactory  object MyLogger extends LoggerLike {    val factory = if (Play.isTest(Play.current)) {     new SimpleLoggerFactory()   } else {     LoggerFactory.getILoggerFactory   }    val redirectDebugToInfo = factory.isInstanceOf[SimpleLoggerFactory]    val logger = factory.getLogger("application")    def apply(name: String): Logger = new Logger(factory.getLogger(name))    def apply[T](clazz: Class[T]): Logger = new Logger(factory.getLogger(clazz.getCanonicalName))    // this method is to make debug statements to show up in test mode   override def debug(m: => String) = {     if (redirectDebugToInfo) {       info(m)     } else {       super.debug(m)     }   } } 

I don't know how this code behaves regarding the PermGen leak in general, but so far I didn't have that problem. To make it work you need to add this dependency:

"org.slf4j" % "slf4j-simple" % "1.6.4" 
like image 182
rintcius Avatar answered Sep 28 '22 00:09

rintcius