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));
}
}
@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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With