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: 
        ${t.getMessage}")
    }    
  }
}

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">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%markerSimpleName %-5p 
        %C.%M():%L - %msg %ex{full}%n"/>
    </Console>
    <File name="Log" fileName="./logs/App.log">
      <PatternLayout>
        <Pattern>%markerSimpleName %-5p %C.%M():%L 
          - %msg %ex{full}%n</Pattern>
      </PatternLayout>
    </File>    
  </Appenders>
  <Loggers>
    <Logger name="org.henrietteharmse.tutorial" 
      level="trace" additivity="false">
      <AppenderRef ref="Log"/>
    </Logger>
    <Root level="error">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>

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 := 
  "org.henrietteharmse.tutorial"

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

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

Conclusion

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.

Setting up Virtuoso on Ubuntu 18.04

I recently needed to setup a local instance of Virtuoso on my Ubuntu 18.04 laptop. In particular I needed to be able to specify the directory that Virtuoso is installed. This meant that using a Linux package manager was not an option. I therefore opted to install Virtuoso from source.

  1. Here are the steps I followed to do this:
    We are using Virtuoso 7.2.2. in our production environment, hence I downloaded virtuoso-opensource-7.2.2.tar.gz from Virtuoso downloads.
  2. Then I followed the instructions to build Virtuoso from Building from Upstream Source.
    1. Check build environment by running the following commands:
      sudo apt-get install dpkg-dev build-essential 
      sudo apt-get install autoconf automake libtool flex bison gperf gawk m4 make odbcinst libxml2-dev libssl-dev libreadline-dev
      
    2. Extract the file:
      tar xvpfz virtuoso-opensource-7.2.2.tar.gz
    3. Now to configure Virtuoso to install to a directory of your choice, in the directory you extracted the .tar.gz, run the following:
      ./configure --prefix=directory_of_choice
      
    4. To build Virtuoso, run:
      make nice
      

      This step failed for me with an error message that looks something like the following

      make[3]: Entering directory '~/virtuoso-opensource/libsrc/Wi'
      /bin/bash ../../libtool  --tag=CC   --mode=compile gcc -DHAVE_CONFIG_H -I. -I../../libsrc/Dk    -fno-strict-aliasing -O2  -Wall  -DNDEBUG -DPOINTER_64   -I/home/klimek/virtuoso-opensource/libsrc/Xml.new  -DOPENSSL_NO_KRB5 -Dlinux -D_GNU_SOURCE -DFILE64 -D_LARGEFILE64_SOURCE   -I../../libsrc -I../../libsrc/Dk -I../../libsrc/zlib -I. -I../../libsrc/langfunc -I../../libsrc/plugin -I../../libsrc/Tidy -I../../libsrc/Xml.new -I../../libsrc/odbcsdk/include -DVAD -DDBP -DBIF_XPER -DOPSYS=\"Linux\" -DHOST=\"x86_64-unknown-linux-gnu\" -g -O2 -MT libwi_la-bif_crypto.lo -MD -MP -MF .deps/libwi_la-bif_crypto.Tpo -c -o libwi_la-bif_crypto.lo `test -f 'bif_crypto.c' || echo './'`bif_crypto.c
      libtool: compile:  gcc -DHAVE_CONFIG_H -I. -I../../libsrc/Dk -fno-strict-aliasing -O2 -Wall -DNDEBUG -DPOINTER_64 -I/home/klimek/virtuoso-opensource/libsrc/Xml.new -DOPENSSL_NO_KRB5 -Dlinux -D_GNU_SOURCE -DFILE64 -D_LARGEFILE64_SOURCE -I../../libsrc -I../../libsrc/Dk -I../../libsrc/zlib -I. -I../../libsrc/langfunc -I../../libsrc/plugin -I../../libsrc/Tidy -I../../libsrc/Xml.new -I../../libsrc/odbcsdk/include -DVAD -DDBP -DBIF_XPER -DOPSYS=\"Linux\" -DHOST=\"x86_64-unknown-linux-gnu\" -g -O2 -MT libwi_la-bif_crypto.lo -MD -MP -MF .deps/libwi_la-bif_crypto.Tpo -c bif_crypto.c  -fPIC -DPIC -o .libs/libwi_la-bif_crypto.o
      bif_crypto.c: In function ‘box_hmac’:
      bif_crypto.c:184:12: error: storage size of ‘ctx’ isn’t known
         HMAC_CTX ctx;
                  ^~~
      bif_crypto.c:190:3: warning: ‘HMAC_Init’ is deprecated [-Wdeprecated-declarations]
         HMAC_Init (&ctx, key, box_length (key) - DV_STRINGP (key) ? 1 : 0, md);
       ...

      which I fixed by installing libssl1.0-dev with

      sudo apt-get install libssl1.0-dev
      

      You can read more on this error here.
      After this re-run make nice.

    5. Lastly, I ran
      make install
      

      because I installed Virtuoso in my home directory. If you are not installing it in you home directory, you will need to run

      sudo make install
      
  3. To test your installation, go to localhost:8890.