Logback integration module for Play Framework that provides logging configuration and colored console output
npx @tessl/cli install tessl/maven-com-typesafe-play--play-logback_2-11@2.7.0Play Logback is a Logback integration module for the Play Framework that provides seamless logging configuration and colored console output. It implements Play's LoggerConfigurator interface to bridge the gap between Play applications and Logback's powerful logging infrastructure.
com.typesafe.play:play-logback_2.11SBT:
libraryDependencies += "com.typesafe.play" %% "play-logback" % "2.7.9"Maven:
<dependency>
<groupId>com.typesafe.play</groupId>
<artifactId>play-logback_2.11</artifactId>
<version>2.7.9</version>
</dependency>import play.api.libs.logback.LogbackLoggerConfigurator
import play.api.libs.logback.ColoredLevel
import play.api.{Environment, Configuration, Mode}Play Logback is typically used automatically by the Play Framework through its service discovery mechanism. It requires no direct instantiation by application developers:
// The LogbackLoggerConfigurator is automatically discovered and used
// by Play Framework during application startup via logger-configurator.properties
// However, if manual configuration is needed:
import play.api.libs.logback.LogbackLoggerConfigurator
import play.api.{Environment, Configuration, Mode}
import java.io.File
val configurator = new LogbackLoggerConfigurator()
val env = Environment(new File("."), getClass.getClassLoader, Mode.Dev)
// Initialize logging
configurator.configure(env, Configuration.empty, Map.empty)
// Later, shutdown logging
configurator.shutdown()Play Logback provides two main components:
The module integrates with Play's application lifecycle and supports multiple configuration discovery mechanisms, making it flexible for different deployment scenarios.
The LogbackLoggerConfigurator class provides comprehensive Logback integration for Play Framework applications.
class LogbackLoggerConfigurator extends LoggerConfigurator {
def loggerFactory: org.slf4j.ILoggerFactory
def init(rootPath: java.io.File, mode: play.api.Mode): Unit
def configure(env: play.api.Environment): Unit
def configure(env: play.api.Environment, configuration: play.api.Configuration, optionalProperties: Map[String, String]): Unit
def configure(properties: Map[String, String], config: Option[java.net.URL]): Unit
def shutdown(): Unit
}Key Methods:
loggerFactory: Returns the underlying SLF4J logger factory instance (Logback's LoggerContext)init: Initialize logging when no application ClassLoader is available, using simple file path and modeconfigure (simple): Configure with environment only, using empty configuration and no optional propertiesconfigure (full): Configure with complete application context including environment, configuration, and optional propertiesconfigure (low-level): Configure with pre-assembled properties map and optional configuration URLshutdown: Properly shutdown the logging infrastructure, including SLF4J bridge cleanupConfiguration Discovery:
The configurator searches for configuration in this order:
logger.resource - classpath resource pathlogger.file - file system pathlogger.url - direct URLlogback-test.xml (Test mode only)logback.groovylogback.xmllogback-play-dev.xml (Dev mode) or logback-play-default.xml (other modes)Built-in Configuration Files:
The module includes three pre-configured Logback configuration files:
org.jdbcdslog loggersThread Safety:
All configuration operations are synchronized on the logger factory instance to prevent concurrent modification issues during application testing.
The ColoredLevel converter provides colored log level output for improved readability during development.
class ColoredLevel extends ch.qos.logback.classic.pattern.ClassicConverter {
def convert(event: ch.qos.logback.classic.spi.ILoggingEvent): String
}Color Mapping:
[trace] in blue[debug] in cyan[info] in white[warn] in yellow[error] in redUsage in Logback Configuration:
<configuration>
<conversionRule conversionWord="coloredLevel" converterClass="play.api.libs.logback.ColoredLevel" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%coloredLevel %logger{15} - %message%n%xException{10}</pattern>
</encoder>
</appender>
</configuration>SQL Logging Configuration:
The logback-play-logSql.xml configuration provides specialized SQL logging support with both file and console output:
<configuration>
<conversionRule conversionWord="coloredLevel" converterClass="play.api.libs.logback.ColoredLevel" />
<!-- File logging -->
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${application.home:-.}/logs/application.log</file>
<encoder>
<pattern>%date [%level] from %logger in %thread - %message%n%xException</pattern>
</encoder>
</appender>
<!-- JDBC logging configuration -->
<logger name="org.jdbcdslog.ConnectionLogger" level="OFF" /> <!-- Won't log connections -->
<logger name="org.jdbcdslog.StatementLogger" level="INFO" /> <!-- Will log all statements -->
<logger name="org.jdbcdslog.ResultSetLogger" level="OFF" /> <!-- Won't log result sets -->
</configuration>JUL Bridge Configuration:
The LogbackLoggerConfigurator automatically:
Property Management:
Supports dynamic property injection:
application.home property from environment root pathplay.logger.includeConfigProperties=trueService Discovery:
Registered via logger-configurator.properties:
play.logger.configurator=play.api.libs.logback.LogbackLoggerConfiguratorThis enables automatic discovery by Play Framework's LoggerConfigurator.apply() method during application startup.
// From Play Framework Core (dependencies)
trait LoggerConfigurator {
def init(rootPath: java.io.File, mode: Mode): Unit
def configure(env: Environment): Unit
def configure(env: Environment, configuration: Configuration, optionalProperties: Map[String, String]): Unit
def configure(properties: Map[String, String], config: Option[java.net.URL]): Unit
def loggerFactory: org.slf4j.ILoggerFactory
def shutdown(): Unit
}
sealed trait Mode
object Mode {
case object Dev extends Mode
case object Prod extends Mode
case object Test extends Mode
}
case class Environment(
rootPath: java.io.File,
classLoader: ClassLoader,
mode: Mode
)
case class Configuration(underlying: com.typesafe.config.Config)Configuration Errors:
If no Logback configuration is found, the configurator prints an error message to System.err but continues execution:
Could not detect a logback configuration file, not configuring logbackLogback Status Messages:
Configuration errors and warnings are automatically printed via ch.qos.logback.core.util.StatusPrinter.printIfErrorsOccured().
Service Discovery Failures:
If the LogbackLoggerConfigurator class cannot be loaded, Play Framework falls back to no-op logging configuration with appropriate error messages.
ch.qos.logback:logback-classic:1.2.3) - Core logging implementationorg.slf4j:slf4j-api) - Logging facade interfacecom.typesafe.play:play) - LoggerConfigurator interface, utilities, and Colors utility for ColoredLevel converter