尽管配置了回滚,但Play 2.6x Scala应用仍未记录

时间:2018-10-22 01:07:51

标签: scala playframework

我正在尝试启用Play 2.6 Scala应用程序的登录功能。

我基本上已经实现了此日志记录过滤器来记录服务器处理的每个请求:https://www.playframework.com/documentation/2.6.x/ScalaHttpFilters

我一生都无法将日志显示在控制台或特定的log/目录中。我尝试过使用logback.xml文件和应用程序配置进行的所有操作。

有人对我要去哪里有什么想法吗?我编写的过滤器正在应要求进行处理(我设置了一个断点以确保)。

logback.xml:

<!-- https://www.playframework.com/documentation/latest/SettingsLogger -->
<configuration>

    <conversionRule conversionWord="coloredLevel" converterClass="play.api.libs.logback.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 - %message%n%xException</pattern>
        </encoder>
    </appender>

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

    <appender name="ASYNCFILE" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="FILE" />
    </appender>

    <appender name="ASYNCSTDOUT" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="STDOUT" />
    </appender>

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

    <!-- Off these ones as they are annoying, and anyway we manage configuration ourselves -->
    <logger name="com.avaje.ebean.config.PropertyMapLoader" level="OFF" />
    <logger name="com.avaje.ebeaninternal.server.core.XmlConfigLoader" level="OFF" />
    <logger name="com.avaje.ebeaninternal.server.lib.BackgroundThread" level="OFF" />
    <!--<logger name="org.jdbcdslog.StatementLogger"  level="DEBUG" /> &lt;!&ndash; Will log all statements &ndash;&gt;-->
    <!--<logger name="slick.jdbc.JdbcBackend.statement"  level="DEBUG" />-->

    <logger name="com.gargoylesoftware.htmlunit.javascript" level="OFF" />

    <root level="INFO">
        <appender-ref ref="ASYNCFILE" />
        <appender-ref ref="ASYNCSTDOUT" />
    </root>

</configuration>

LoggingFilter.scala:

package filters

import javax.inject.Inject
import akka.stream.Materializer
import play.api.Logger
import play.api.mvc._
import scala.concurrent.{ExecutionContext, Future}

class LoggingFilter @Inject() (implicit val mat: Materializer, ec: ExecutionContext) extends Filter {

  def apply(nextFilter: RequestHeader => Future[Result])
           (requestHeader: RequestHeader): Future[Result] = {

    Logger.debug("Hi!") // Why you no work!!

    val startTime = System.currentTimeMillis

    nextFilter(requestHeader).map { result =>

      val endTime = System.currentTimeMillis
      val requestTime = endTime - startTime

      Logger.info(s"${requestHeader.method} ${requestHeader.uri} took ${requestTime}ms and returned ${result.header.status}")

      result.withHeaders("Request-Time" -> requestTime.toString)
    }
  }
}

1 个答案:

答案 0 :(得分:0)

好吧,经过几个小时的调试,我终于找到了原因(可能对其他人有帮助,请在此处发布)。

原因:

此问题的根本原因是play-silhoutte中捆绑的日志记录依赖项冲突。我的应用程序使用的是play-silhoutte版本5.0.3,显然包含一个错误,该错误导致Play记录器在play-silhouttecom.typesafe.play:play-openid_2.12:2.6.9)的从属关系中被日志记录配置覆盖。

诊断:

不幸的是,这花费了很长时间进行调试-结合读取Play源代码+在初始化Logger时调试堆栈帧。我注意到记录器已被初始化两次-一次是为实际的Play记录器初始化的(使用我的logback.xml中指定的正确配置,然后是play-silhoutte的结果(有效覆盖)好的记录器。

看着初始化参数,它正在从logback.xml读取其/Users/.../.ivy2/cache/com.typesafe.play/play-openid_2.12/jars/play-openid_2.12-2.6.9.jar!logback.xml。我扩展了jar并找到了以下日志配置:

<!--
  ~ Copyright (C) 2009-2017 Lightbend Inc. <https://www.lightbend.com>
  -->
<configuration>
    <!-- Suppress logback complaining about multiple logback-test.xml files -->
    <statusListener class="ch.qos.logback.core.status.NopStatusListener" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <!-- We use short exception stack trace logging to limit output for travis. -->
            <!-- Change to full if you need to do further debugging, but never commit that. -->
            <pattern>%level %logger{15} - %message%n%ex{short}</pattern>
        </encoder>
    </appender>

    <root level="WARN">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

在这一点上,我知道这是一个相互矛盾的礼节问题。

解决方案:

进行一些挖掘之后,我实际上发现了bug open on Github,描述了我的问题。我正在运行play-silhoutte版本5.0.3,因此将其更改为5.0.4可以解决此问题。