Usage

Using the Scala API is as simple as mixing in the Logging trait to your class. Example:

import org.apache.logging.log4j.scala.Logging
import org.apache.logging.log4j.Level

class MyClass extends BaseClass with Logging {
  def doStuff(): Unit = {
    logger.info("Doing stuff")
  }
  def doStuffWithLevel(level: Level): Unit = {
    logger(level, "Doing stuff with arbitrary level")
  }
  def doStuffWithUser(user: User): Unit = {
    logger.info(s"Doing stuff with ${user.getName}.")
  }
}

API Documentation

Scala Version API Link

Scala 2.10

ScalaDocs

Scala 2.11

ScalaDocs

Scala 2.12

ScalaDocs

Scala 2.13

ScalaDocs

Configuration

Log4j Scala API uses Log4j configuration by default. This supports XML, properties files, and Java-based builders, as well as JSON and YAML with additional dependencies.

Substituting Parameters

Unlike in Java, Scala provides native functionality for string interpolation beginning in Scala 2.10. As all logger calls are implemented as macros, using string interpolation directly does not require additional if checks. For example:

logger.debug(s"Logging in user ${user.getName} with birthday ${user.calcBirthday}")

Logger Names

Most logging implementations use a hierarchical scheme for matching logger names with logging configuration. In this scheme the logger name hierarchy is represented by '.' characters in the logger name, in a fashion very similar to the hierarchy used for Java/Scala package names. The Logger property added by the Logging trait follows this convention: the trait ensures the Logger is automatically named according to the class it is being used in.