Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging in Unit Tests for Play 2.3

Been struggling to get any logging output in unit tests in Play 2.3 using Logger.error("an error"). I've tried all of these examples which seem to be outdated.

The closest I got was with a build.sbt containing the following:

testOptions in Test += Tests.Argument("-Dlogger.resource=conf/test-logger.xml")

This causes the test-logger.xml file to configure the logger, but still I do not get output from any tests.

Has anyone had any success with this in Play 2.3?

Temporary Workaround

I'm currently using a quick util class to deal with this until it is fixed. Added here as it may be useful for someone else.

public class Logger {

    public static void debug(String message) {
        System.out.println(String.format("[debug] %s", message));
    }

    public static void info(String message) {
        System.out.println(String.format("[info] %s", message));
    }

    public static void warn(String message) {
        System.out.println(String.format("[warn] %s", message));
    }

    public static void error(String message) {
        System.err.println(String.format("[error] %s", message));
    }
}
like image 239
Tony Day Avatar asked Aug 07 '14 12:08

Tony Day


3 Answers

@Jamesinchina updated his answer (in the question I referenced) to support Play Framework 2.3:

I edited the answer to remove play.Project which is removed in 2.3.x, but the remaining still works - we are currently using it in our project.

Adding a conf/test-logger.xml file and the following line in build.sbt does the trick:

javaOptions in Test += "-Dlogger.file=conf/test-logger.xml"

Updated answer

like image 180
Tony Day Avatar answered Nov 12 '22 04:11

Tony Day


Since Play 2.5 everything is configured only via logback so we can rely on the default logback mechanisms to find the configs: http://logback.qos.ch/manual/configuration.html.

This means that inside the <project_or_module>/test/resources folder one can place a file called logback-test.xml which takes precedence over the default logback.xml since both are on the class path when running tests.

like image 45
Fabian Avatar answered Nov 12 '22 06:11

Fabian


You could try to add this to your build.sbt:

testOptions += Tests.Argument(TestFrameworks.JUnit, "-v", "-q")

Check this page: https://groups.google.com/forum/#!topic/play-framework/WiqpjWZ_Qt0

like image 1
AndersHA Avatar answered Nov 12 '22 05:11

AndersHA