gpt4 book ai didi

java - 玩!框架日志记录仅适用于系统级别

转载 作者:行者123 更新时间:2023-11-30 05:39:52 29 4
gpt4 key购买 nike

我目前正在与 Play 合作!框架和看来日志记录只适用于游戏!仅但对于具有 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)

最佳答案

尝试将根记录器级别从 WARN 更改为 INFO,如下所示

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

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

If a given logger is not assigned a level, then it inherits one from its closest ancestor with an assigned level.

此处 controllers.search.SearchController 从 root 继承了 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" />

关于java - 玩!框架日志记录仅适用于系统级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55876819/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com