Henriette's Notes

Home » Posts tagged 'Scala'

Tag Archives: Scala

scala-logging with log4j2

In this brief post I provide a minimal complete example that uses scala-logging with log4j2. scala-logging is a Scala logging library wrapping SLF4J.

Scala code

Here is a minimal complete Scala code example using scala-logging. Note that we use a companion object to define an instance of Logger that will be shared across all instances of SimpleLoggingTest.

package org.henrietteharmse.tutorial

import com.typesafe.scalalogging.Logger

class SimpleLoggingTest {
  SimpleLoggingTest.logger.trace("Hello while instance
    of SimpleLoggingTest is created.")

object SimpleLoggingTest {
  private val logger = Logger[SimpleLoggingTest]

  def main(args: Array[String]):Unit = {
    logger.trace("Hello from SimpleLoggingTest 
      companion object")
    val simpleLoggingTest = new SimpleLoggingTest
    try {
      throw new RuntimeException("Some error")
    } catch {
      case t : Throwable => logger.error(s"Error: 

Log4j configuration

In this section we provide a minimal log4j.xml configuration file. It defines a trace level logger for our application code with an error level logger for all other errors. Logging for our code will be written to a file in ./logs/App.log, and all errors, originating from other code than our own code, being logged to the console.

<?xml version="1.0" encoding="UTF-8"?>
  <Configuration status="WARN">
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%markerSimpleName %-5p 
        %C.%M():%L - %msg %ex{full}%n"/>
    <File name="Log" fileName="./logs/App.log">
        <Pattern>%markerSimpleName %-5p %C.%M():%L 
          - %msg %ex{full}%n</Pattern>
    <Logger name="org.henrietteharmse.tutorial" 
      level="trace" additivity="false">
      <AppenderRef ref="Log"/>
    <Root level="error">
      <AppenderRef ref="Console"/>

SBT configuration

The build.sbt file for building this application is given below. Most importantly it states the correct dependencies for using scala-logging with the libraryDependencies setting. To get debug information from log4j, we set the system property log4j2.debug to true.
ThisBuild / scalaVersion := "2.13.2"
ThisBuild / organization := 

val setLog4jDebug = sys.props("log4j2.debug") = "true"

lazy val root = (project in file("."))
  name := "scala-logging-with-log4j2",
  libraryDependencies ++= Seq(
    "com.typesafe.scala-logging" %% "scala-logging" % 
    "org.slf4j" % "slf4j-api" % "1.7.30",
    "org.apache.logging.log4j" % "log4j-slf4j-impl" % 
  scalacOptions ++= Seq("-deprecation")


This post gives a minimal complete example for using scala-logging with log4j using SBT. The complete code example can be found here on GitHub.

DBPedia Extraction Framework and Eclipse Quick Start

I recently treid to compile the DBPedia Extraction Framework. What was not immediately clear to me is whether I have to have Scala installed. It turns out that having Scala installed natively is not necessary, seeing as the scala-maven-plugin is sufficient.

The steps to compile DBPedia Extraction Framework from the command line are:

  1. Ensure you have the JDK 1.8.x installed.
  2. Ensure Maven 3.x is installed.
  3. mvn package

Steps to compile DBPedia Extraction Framework from the Scala IDE (which can be downloaded from Scala-ide.org) are:

  1. Ensure you have the JDK 1.8.x installed.
  2. Ensure you have the Scala IDE installed.
  3. mvn eclipse:eclipse
  4. mvn package
  5. Import existing Maven project into Scala IDE.
  6. Run mvn clean install from within the IDE.