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 a default logger:
import play.api.Logger object Task{ def delete(id:Long) = { logger.debug(s"deleting task with id $id") ... } } - By using a logger with its class name:
import play.api.Logger...