Log4j Scala API
Log4j Scala API provides a Scala-friendly interface to log against the Log4j API.
It supports Scala 2.10
, 2.11
, 2.12
, 2.13
, and 3
.
This is just a logging API. Your application still needs to have a logging backend (e.g., Log4j) configured. |
Dependencies
You need to have the org.apache.logging.log4j:log4j-api-scala
dependency in your classpath:
libraryDependencies ++= Seq(
"org.apache.logging.log4j" %% "log4j-api-scala" % "13.2.0-SNAPSHOT"
)
Java module name is set to org.apache.logging.log4j.api.scala
.
OSGi Bundle-SymbolicName
s are set to org.apache.logging.log4j.api.scala.2.10
, org.apache.logging.log4j.api.scala.2.11
, and so on.
Usage
Using the Scala API is as simple as mixing in the Logging
trait to your class:
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}.")
}
}
Parameter substitution
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:
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.