Play Framework 2.2 log output to console in tests

2019-03-24 15:36发布

How do I configure the logger in Play Framework 2.2 in order that I see log output in the console during play integration tests?

The configuration for Play Framework 2.1 does not work anymore.

BR Rene

2条回答
Rolldiameter
2楼-- · 2019-03-24 16:21

Update To understand more about how logback is configured you should pass -Dlogback.debug=true property to the jvm/play. This might save you hours of debbugging.

Add a file in test/logback-test.xml (needs to be on classpath so it might depend on how the play application is configured to find tests resources) with a content like

<configuration>

    <conversionRule conversionWord="coloredLevel"
        converterClass="play.api.Logger$ColoredLevel" />

    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>${application.home:-.}/logs/application.log</file>
        <encoder>
            <pattern>%date - [%level] - from %logger in %thread
                %n%message%n%xException%n</pattern>
        </encoder>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%coloredLevel %logger{15} - %message%n%xException{5}</pattern>
        </encoder>
    </appender>

    <logger name="play" level="INFO" />
    <logger name="application" level="INFO" />

    <root level="ERROR">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>

</configuration>
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-03-24 16:31

I have not done this yet, but you have to configure a logback configuration file. See the play documentation for additional information: http://www.playframework.com/documentation/2.2.x/SettingsLogger

Once you defined a specific logback configuration file, this needs to be placed in the test package, see this issue for details:https://github.com/playframework/playframework/issues/1669#issuecomment-24452132

See message from @benmccann: you can configure the logger in test mode by placing a logback-test.xml in test/resources (took me hours to figure out!).

查看更多
登录 后发表回答