gpt4 book ai didi

java - Scala Play 框架 : logger pattern for displaying file and line

转载 作者:行者123 更新时间:2023-11-30 10:49:02 25 4
gpt4 key购买 nike

我有一个简单的 Scala 应用程序(未使用 Play MVC 框架)但它使用了 play.api.Logger

我正在尝试找出我需要添加的模式以包括完成日志的文件和行。

这是我从一开始就使用的 logback.xml:

<configuration>

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

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date %-16coloredLevel %message %n</pattern>
</encoder>
</appender>

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

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

</configuration>

它会显示如下内容:

2016-02-22 19:20:05,901 [debug] MY-LOG-MESSAGE

所以我尝试使用 docs 进行更改,然后我得出了类似的结论

<pattern>%date %-16coloredLevel %F L.%L:: %message %n</pattern>

产生的

2016-02-22 22:26:49,725 [debug] Logger.scala L.74:: MY-LOG-MESSAGE

它将文件报告为 play.api.Logger 类文件和相应的行。

我需要写什么才能得到类似 com.package.Clazz.scala L.10 的东西?

编辑

marcospereira answer 的输出:

有这样的东西:

2016-02-23 11:39:36,624 [信息] play.api.LoggerLike$class L.93::MESSAGE

最佳答案

您需要的已详细记录为 Logback log layouts :

c{length}, lo{length}logger{length}

Outputs the name of the logger at the origin of the logging event. This conversion word takes an integer as its first and only option. The converter's abbreviation algorithm will shorten the logger name, usually without significant loss of meaning. Setting the value of length option to zero constitutes an exception. It will cause the conversion word to return the sub-string right to the rightmost dot character in the logger name.

L:

Outputs the line number from where the logging request was issued.

Generating the line number information is not particularly fast. Thus, its use should be avoided unless execution speed is not an issue.

因此,您需要做的是编辑您的 conf/logback.xml 文件来更改日志模式,如下所示:

<pattern>%date %-16coloredLevel %logger L.%L:: %message %n</pattern>

注意日志模式的 %logger 部分。

这里可能存在的问题是,如果您直接使用记录器帮助程序 (play.api.Logger)(每个实例为 Logger.info),类将为 play.api.LoggerLike,这是为其创建日志的类。当然,这对你的用例没有用,所以你可以像这样使用日志:

斯卡拉:

import play.api.Logger
import play.api.mvc.Controller

val logger = Logger(this.getClass)

logger.info("Logging a message")

Java:

import play.Logger;
import play.Logger.ALogger;
class Something {
private static final ALogger logger = Logger.of(Something.class);

public void whatever() {
...
logger.info("some message");
...
}
}

关于java - Scala Play 框架 : logger pattern for displaying file and line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35565385/

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