在启动自动测试时在Play Framework中进行配置记录

时间:2011-12-02 16:28:37

标签: java log4j playframework

我在Play Framework应用程序中配置了日志记录。当我在prod或dev模式下运行应用程序或通过命令运行测试时(播放测试) - 一切正常,但是当我运行'play auto-test'时,测试无法执行。请帮忙!

在application.conf中:

application.log=INFO
application.log.path=/log4j.xml 

的log4j.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="file" class="org.apache.log4j.RollingFileAppender">
      <param name="File" value="${application.path}/logs/application.log"/>
      <param name="MaxFileSize" value="1MB"/>
      <param name="MaxBackupIndex" value="100"/>
      <layout class="org.apache.log4j.PatternLayout">
          <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p ~ %m %n"/>
      </layout>
  </appender>
 <logger name="play">
     <level value="error"/>
 </logger>
 <root>
     <priority value="error"/>
     <appender-ref ref="file"/>
 </root>
</log4j:configuration>

当我使用以下log4j.xml文件时:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="console" class="org.apache.log4j.ConsoleAppender">
      <param name="Target" value="System.out"/>
      <layout class="org.apache.log4j.PatternLayout">
          <param name="ConversionPattern" value="%m%n"/>
      </layout>
  </appender>
 <logger name="play">
     <level value="debug"/>
 </logger>
 <root>
     <priority value="info"/>
     <appender-ref ref="console"/>
 </root>
</log4j:configuration>     

运行命令'play auto-test'的测试已成功执行。请告诉我们如何在“播放自动测试”中配置在Play(输出到文件)中运行并执行测试的日志!

2 个答案:

答案 0 :(得分:1)

这几乎是一个游戏!错误。这不是因为你已经使用单独的文件配置了log4j这一事实,尽管这样做会隐藏另一条错误消息:“play.tmp为null,应该是play.tmp = none”(喜欢这样的东西 - 你'如果你删除你的custon log4j.xml文件并重新做“播放自动测试”,我会得到这个。问题是,即使你将其设置为play.tmp = none,它仍然无效。此外,如果你将他们的在线文档与Play!的控制台输出进行比较,你就会意识到出了问题:

文档说:

“'auto-test'命令与'test'命令相同,但它会自动启动浏览器,运行所有测试并停止。”

控制台sais(当你进行“自动测试”时):

ATTENTION: You're running Play! in DEV mode
~
~ Go to http://localhost:9000/@tests to run the tests
~

答案 1 :(得分:1)

答案非常简单

在application.conf中:

%test.application.log=INFO
%test.application.log.path=/log4j.xml

application.log=INFO
application.log.path=/log4j.properties
application.log.system.out=off

的log4j.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="console" class="org.apache.log4j.ConsoleAppender">
      <param name="Target" value="System.out"/>
      <layout class="org.apache.log4j.PatternLayout">
          <param name="ConversionPattern" value="%m%n"/>
      </layout>
  </appender>
 <logger name="play">
     <level value="debug"/>
 </logger>
 <root>
     <priority value="info"/>
     <appender-ref ref="console"/>
 </root>
</log4j:configuration>

log4j.properties:

log4j.rootLogger=ERROR, Rolling
log4j.logger.play=INFO
log4j.appender.Rolling=org.apache.log4j.RollingFileAppender
log4j.appender.Rolling.File=${application.path}/logs/application.log
log4j.appender.Rolling.MaxFileSize=1MB
log4j.appender.Rolling.MaxBackupIndex=100
log4j.appender.Rolling.layout=org.apache.log4j.PatternLayout
log4j.appender.Rolling.layout.ConversionPattern=%d{ABSOLUTE} %-5p ~ %m %n
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %-5p ~ %m%n