简体   繁体   中英

Play Framework: Logging time

I've been using the Play Framework but like many things, I haven't come across in defining logging templates that add a timestamp to the log. For services that handle many requests/Akka messages per minute, this renders the logging almost useless. For example, we use the Play logger a bit like this;

Logger.error("could not fulfil request", someException)

Which is how the documentation goes. The documentation does specify, that it is possible to define your own logging headers like;

val exceptionLogger = Logger("exception")
// and then
exceptionLogger.error("while handling some/request", someException)

The log entries resulting from the above don't contain a timestamp of any sort;

[error] exception - while handling some/request ...

There's no mention of the time whatsoever. Now, a workaround (not the prettiest) is something like;

def exceptionLogger = {
    val dt = new DateTime
    Logger(s"$dt exception")
}

Which does the job, but I'm not convinced. Is there a better way to format Play's default logging behaviour?

Play uses Logback, you can configure the log format in conf/logback.xml .

The logs in log/application.log will have a date stamp by default by STDOUT will not. Add %date to the pattern in the STDOUT appender.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM