Play Logback只显示课程'play.Logger $ ALogger'

时间:2014-01-30 14:03:11

标签: logging playframework playframework-2.0 logback

我将下一个 Logback 模式用于Play!框架申请:

%d - [%级别] [%lo {0}] [%class] [%F:%L] [%方法]:%msg%n%ex {full} < / p>

但始终显示为%class

[ play.Logger $ ALogger ]或[ play.api.LoggerLike $ class ]

而不是使用记录器的真实类。

完整的logger.xml文件内容是下一个:

    

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${application.home}/logs/application.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- daily rollover -->
        <fileNamePattern>${application.home}/logs/application.%d{yyyy-MM-dd}.log</fileNamePattern>

        <!-- keep 30 days' worth of history -->
        <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder>
        <pattern>%d -[%level][%lo{0}][%class][%F:%L][%method]: %msg%n%ex{full}</pattern>
        <!--  <pattern> %d{HH:mm:ss.SSS} [%thread] %-5level %class{0} - %msg%n</pattern> -->
        <outputPatternAsHeader>true</outputPatternAsHeader>
    </encoder>
</appender>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%coloredLevel %logger{15} - %message%n%xException{5}</pattern>
    </encoder>
</appender>


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

<root level="ERROR">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE" />
</root>

以及几个输出示例:

2014-01-30 14:57:57,671 - [DEBUG] [application] [play.Logger $ ALogger] [Logger.java:332] [debug]:DBClient :: isAlive:true

2014-01-30 14:57:57,695 - [INFO] [application] [play.Logger $ ALogger] [Logger.java:361] [info]:Applicaton start。

2014-01-30 14:57:57,699 - [INFO] [play] [play.api.LoggerLike $ class] [Logger.scala:90] [info]:应用程序启动(开发)

谢谢!

3 个答案:

答案 0 :(得分:1)

Not sure if you found the answer to your question but I found the following that does the trick:

Play.Logger.underlying().debug("Your debug message"); 

Of course you still have to make sure to provide a custom conf/logger.xml file (as you did) to replace Play's default one (see Play docs on Logging) and include in the pattern element, the %class conversion specifier in the appender (Logback Docs).

Some things to consider before you move forward:

  • I don't know if this could negatively impact any aspect of your app such as portability, etc.
  • Also note that you would have to replace your calls to Play.Logger with Play.Logger.underlying() or create a wrapper class around Play.Logger. Depending on the size of your project this can be unacceptable.

Hope this helps

Source

答案 1 :(得分:0)

内部类Logger.ALogger是实际调用logback logger的播放代码,因此是预期的。

不确定是否可以覆盖,以便它可以访问调用使用记录器的类的类。我想如果你真的想要输出调用类,那么选项是直接在你的代码中使用logback。

答案 2 :(得分:0)

请参阅:https://github.com/playframework/playframework/issues/1669

简单地说:logback的方式是类名,文件名和行名计算是通过“抛出异常”来实现的。并在堆栈跟踪中查找特定帧。但是通过使用play的API而不是直接的logback API,这样索引的帧属于播放代码。