9

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));
    }
}
Community
  • 1
  • 1
Tony Day
  • 2,170
  • 19
  • 25

3 Answers3

9

@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

Community
  • 1
  • 1
Tony Day
  • 2,170
  • 19
  • 25
6

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.

Fabian
  • 2,428
  • 2
  • 21
  • 19
1

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

AndersHA
  • 91
  • 2
  • 4
  • This provides better output to console, but does not include logging output when using `play.Logger.error("a error")` - even with `testOptions in Test += Tests.Argument("-Dlogger.resource=test-logger.xml")` included as well. – Tony Day Aug 08 '14 at 08:29
  • Perhaps not exactly what the OP was asking for, but close enough, and was what I was looking for, and google led me to this QA.. so cudos earnt :) – Kaos Jan 18 '18 at 08:16