Play's logging API

Play exposes the logging API through play.api.Logger. Let's have a look at the class and object definition of it:

class Logger(val logger: Slf4jLogger) extends LoggerLike

object Logger extends LoggerLike {

  ...
  val logger = LoggerFactory.getLogger("application")

  def apply(name: String): Logger = new Logger(LoggerFactory.getLogger(name))

  def apply[T](clazz: Class[T]): Logger = new Logger(LoggerFactory.getLogger(clazz))

  ...

}

The LoggerLike trait is just a wrapper over Slf4jLogger. By default, all application logs are mapped to Logger with the application name and the Play-related logs are mapped to Logger with the Play name.

After importing play.api.Logger, we can use the default logger or define a custom one in these ways:

  • By using ...

Get Mastering Play Framework for Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.