Scala Play框架:用于显示文件和行的记录器模式 [英] Scala Play framework: logger pattern for displaying file and line

查看:108
本文介绍了Scala Play框架:用于显示文件和行的记录器模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've a simple scala app (not using Play MVC framework) but that uses play.api.Logger

我试图弄清楚我需要添加的模式,以包括完成日志的文件和行.

I'm trying to figure out the pattern I need to add to include the File and Line where the log was done.

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

This was the logback.xml I was using from the start:

<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 进行更改,结果出现了类似

So I tried to change using the docs, and I came to something like

<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类文件和相应的行.

It is reporting the File as the play.api.Logger class file and the corresponding line.

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

What do I need to write to I could get something like com.package.Clazz.scala L.10 ?

marcospereira答案的输出:

有类似的内容:

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

推荐答案

您所需要的文件已被详细记录为 Logback日志布局:

What you need is well documented as Logback log layouts:

在记录事件的起源处输出记录器的名称. 此转换字将整数作为其第一个也是唯一的选择.转换器的缩写算法将缩短记录器名称,通常不会造成重大意义损失.将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.

Lline:

L OR 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文件,以更改日志模式,如下所示:

So, what you need to do is edit your conf/logback.xml file to alter the log pattern like this:

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

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

此处可能存在的问题是,如果您直接使用记录器帮助程序(play.api.Logger)(每个实例Logger.info),则该类将为play.api.LoggerLike,这是该类的日志已创建.当然,这对您的用例没有用,因此您可以使用如下日志:

A possible problem here is that, if you are using the logger helper (play.api.Logger) directly (Logger.info, per instance), the the class will be play.api.LoggerLike, which is the class for which the log was created. Of course, this is not useful for your use case, so you can use the log like this:

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框架:用于显示文件和行的记录器模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆