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.javadsl.logging;

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

public class LoggingExample {
  private final Logger log = LoggerFactory.getLogger(LoggingExample.class);

  public void demonstrateLogging(String msg) {
    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(LagomJava)

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.javadsl.logging;

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

public class Log4j2Example {
  private static final Logger LOGGER = LogManager.getLogger();

  public void demonstrateLogging(String msg) {
    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(LagomJava, 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.