How do I output the class name in PlayFramework 2

2019-05-28 14:45发布

问题:

I setup a logging.xml file as shown below. This file includes output to the console as well as a rolling file which gets a new file for everyday:

<configuration>
<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">
        <FileNamePattern>${application.home}/logs/application.%d{yyyy-MM-dd}.log</FileNamePattern>
    </rollingPolicy>

    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</Pattern>
    </layout>
</appender>
<appender name="A1" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%p %d{ISO8601} %c - %m%n</pattern>
    </encoder>
</appender>
<logger name="javax.faces" level="debug" />
<root level="info">
    <appender-ref ref="A1" />
    <appender-ref ref="FILE" />
</root>
</configuration>

My question with this is how do I output the class name as well? I tried reading the Play documentation and could not find the answer to that... It's very hard in a production environment to not see classnames as well. Thanks for the help!

回答1:

According to the doc, to get the class name, you should use in your pattern %class{0}



回答2:

The playframework uses slf4j as logging engine and does not print the class name even after the following expression :

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{ISO8601} %-5level [%thread] %logger{36}: %class{0}::%method:%line - %msg%n</pattern>
    </encoder>
</appender>

The fix however is to use :

Logger.underlying().info("My logging message");

to get the classname and method and line number. Output :

2015-12-22 17:43:49,969 INFO [play-akka.actor.default-dispatcher-30] application: MyController::get:82 - My logging message



回答3:

seems problem in your private implementation of PatternLayout class in ch.qos.logback.classic. PatternLayout. Try to use standard log4j implementation. And how are you getting logger instance, as like this: private static Logger logger = Logger.getLogger (MyClass.class); ?



回答4:

Please read the docs carefully. Also play2 uses logback as logging engine, so read more about it on Logback's official web site.