Logging

§Logging

Lagom uses SLF4J for logging, backed by Logback as its default logging engine. Here is a short example showcasing how you can access the logger:

package docs.scaladsl.logging

import org.slf4j.Logger
import org.slf4j.LoggerFactory

class LoggingExample {
  private final val log: Logger =
    LoggerFactory.getLogger(classOf[LoggingExample])

  def demonstrateLogging(msg: String): Unit = {
    log.debug("Here is a message at debug level: {}.", msg)
  }
}

And you can read of more advanced usages on the SLF4J user manual.

If you’re using maven, you’ll need to add the Lagom logback dependency to your classpath to get Logback support:

<dependency>
    <groupId>com.lightbend.lagom</groupId>
    <artifactId>lagom-logback_${scala.binary.version}</artifactId>
    <version>${lagom.version}</version>
</dependency>

If you’re using sbt, you’ll automatically get the Lagom logback support when you enable the Lagom plugin on a project:

lazy val usersImpl = (project in file("usersImpl"))
// Lagom logging module is automatically added to the classpath
  .enablePlugins(LagomScala)

If you’d like to use the Lagom logger module on a project that doesn’t have the Lagom sbt plugin enabled (e.g., a Lagom API project), simply add the Lagom logger module as an explicit library dependency:

// `LagomImport` provides a few handy alias to several Lagom modules
import com.lightbend.lagom.sbt.LagomImport.lagomLogback

lazy val usersApi = (project in file("usersApi"))
  .settings(libraryDependencies += lagomLogback)

The Lagom logger module includes a default logback configuration. Read Configuring Logging for details.

§Log4j 2

Lagom can be configured to use Log4j 2 as its default logging engine. Using Log4j 2 can use either the SLF4J API for logging as shown above or the Log4j 2 API. For example, using the Log4j 2 API:

package docs.scaladsl.logging

import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger

object Log4j2Example {
  private final val Logger: Logger = LogManager.getLogger
}

class Log4j2Example {
  def demonstrateLogging(msg: String): Unit = {
    Log4j2Example.Logger.debug("Here is a message at the debug level: {}", msg)
  }
}

If you’re using maven, you’ll need to add the Lagom log4j2 dependency to your classpath to get Log4j 2 support:

<dependency>
    <groupId>com.lightbend.lagom</groupId>
    <artifactId>lagom-log4j2_${scala.binary.version}</artifactId>
    <version>${lagom.version}</version>
</dependency>

Note that this dependency replaces logback here, so remove any logback dependencies from your pom.xml as well.

If you’re using sbt, you’ll have to disable the LagomLogback plugin and enable the LagomLog4j2 plugin instead. So for example, if you’re using the Lagom plugin:

lazy val blogImpl = (project in file("blogImpl"))
  .enablePlugins(LagomScala, LagomLog4j2)
  .disablePlugins(LagomLogback)

When you’re not using the Lagom plugin, add the Lagom Log4j2 module as a dependency:

// `LagomImport` provides a few handy alias to several Lagom modules
import com.lightbend.lagom.sbt.LagomImport.lagomLog4j2

lazy val blogApi = (project in file("blogApi"))
  .settings(libraryDependencies += lagomLog4j2)

The Lagom Log4j2 module also includes default logging configurations. Read Configuring Logging for details.

Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.