Scala Play 框架:用于显示文件和行的记录器模式
Scala Play framework: logger pattern for displaying file and line
我有一个简单的 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 class 文件和相应的行。
我需要写什么才能得到类似 com.package.Clazz.scala L.10
的东西?
编辑
的输出:
得到类似的东西:
2016-02-23 11:39:36,624 [info] 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
或 line
:
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),class 将是 play.api.LoggerLike
,这是为其创建日志的 class。当然,这对你的用例没有用,所以你可以这样使用日志:
斯卡拉:
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");
...
}
}
我有一个简单的 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 class 文件和相应的行。
我需要写什么才能得到类似 com.package.Clazz.scala L.10
的东西?
编辑
得到类似的东西:
2016-02-23 11:39:36,624 [info] 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
或 line
:
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),class 将是 play.api.LoggerLike
,这是为其创建日志的 class。当然,这对你的用例没有用,所以你可以这样使用日志:
斯卡拉:
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");
...
}
}