Skip to main content

Learn how to use Log4J in Scala

Setup

In order to use Log4J in Scala:

  • Add the library dependency to your POM / SBT (Check if there is a more recent version in Maven Central):
org.apache.logging.log4j:log4j-api:2.18.0
org.apache.logging.log4j:log4j-core:2.18.0
org.apache.logging.log4j:log4j-slf4j-impl:2.18.0

If you’re using Spark, this should already be included, so don’t need to add them.

Usage example

For using it, you can create a singleton object, so it can be easier to access it, or just use the LoggerFactory anywhere you want to use it for creating a logger instance.

package ...

import org.slf4j.LoggerFactory

object Logger {

val logger = LoggerFactory.getLogger("Logger")

}

And use the different methods that it provides:

import ...Logger.logger

logger.debug(s"This is a debug message")

Configuration in .properties

You can also add configuration in the log4j.properties or log4j.xml. For that, add the file in the resources folder:

picture de-500

Using the configuration in a .properties file:

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=ALL, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%7.7r] %d %45.45C - %6.6p - %m%n

If you want to remove Databricks or Spark Logs you can setup their level in the same file:

log4j.logger.com.databricks=OFF
log4j.logger.org.spark=OFF

Configuration in .xml file

Instead of the properties file, you can also use an XML file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">

<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%7.7r] %d - %6.6p - %m%n"/>
</Console>
</Appenders>

<Loggers>
<Root level="info">
<AppenderRef ref="console" />
</Root>
</Loggers>

</Configuration>