玩!框架记录仅适用于系统级别

时间:2019-04-27 02:46:31

标签: java scala logging playframework slf4j

我目前正在与Play合作!框架和似乎日志记录仅在发挥作用!仅适用于taht具有LOGGER初始化的类。

这是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="INFO" />
  <!-- 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="com.gargoylesoftware.htmlunit.javascript" level="OFF" />
  <root level="WARN">
    <!--<appender-ref ref="ASYNCFILE" />-->
    <appender-ref ref="ASYNCSTDOUT" />
  </root>
</configuration>

这是我使用日志记录机制配置的示例类。每次我尝试执行此代码。没有即将发出的日志

package controllers.search

import javax.inject.{Inject, Singleton}
import play.api.Logger

@Singleton
class SearchController @Inject()(cc: ControllerComponents)
  extends AbstractController(cc) {


  protected val LOGGER: Logger = Logger(this.getClass())

  def search(q:String,
             card: String,
             lat: Option[String],
             lng: Option[String]) = Action {

    LOGGER.info("Searching "+card+" For "+q+" ")

}

我非常怀疑这是一个依赖关系问题,因为每次我的应用程序启动时,它都会向我显示此消息。我假设我的日志记录配置正确

[info] p.c.s.AkkaHttpServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

Server started, use Cmd+P to stop


[info] play.api.Play - Application started (Dev)

1 个答案:

答案 0 :(得分:0)

尝试将root记录程序级别从WARN更改为INFO

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

所有记录器inherit from root记录器,并根据Logback docs

  

如果未为给定的记录器分配一个级别,则它将从中继承一个级别   其最接近的祖先,并具有指定的级别。

此处controllers.search.SearchController从根继承了WARN级别,因此没有记录LOGGER.info消息。

我们还可以将根记录器保持在WARN并像这样更改SearchController的级别:

<logger name="controllers.search.SearchController" level="INFO" />
<root level="WARN">
    <appender-ref ref="ASYNCSTDOUT" />
</root>

类似地,显示play日志的原因是由于以下已存在的配置

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