ClassNotFoundException when adding slf4j to Akka

When adding to a Akka project I got the following error java.lang.ClassNotFoundException: akka.event.slf4j.Slf4jLogger. I double checked my configuration and the Akka documentation. The documentation says the following:

Akka provides a logger for SL4FJ. This module is available in the ‘akka-slf4j.jar’. It has one single dependency; the slf4j-api jar. In runtime you also need a SLF4J backend, we recommend Logback.

No success.

After some Googling I found out that the akka-slf4j library is required in addition to the logback-classic library.

These dependencies need to be added to the build.sbt configuration:

libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.2"

libraryDependencies += "com.typesafe.akka" %% "akka-slf4j" % "2.3.6"

This is the relevant part of the Akka configuration:

akka {
	loggers = ["akka.event.slf4j.Slf4jLogger"]
	loglevel = "DEBUG"
	logging-filter = "akka.event.slf4j.Slf4jLoggingFilter"
	...
}

To be complete, this is the logback.xml configuration:

<configuration>
	<!-- Properties are set below, alternatively they can be set as system 
	     properties or passed as command line arguments -->
	<property name="LOG_HOME" value="logs" />
	<property name="LOG_FILE_NAME" value="application.log" />

	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>%date{ISO8601} %-5level %logger{36} %X{sourceThread} - %msg%n
			</pattern>
		</encoder>
	</appender>

	<appender name="FILE" class="ch.qos.logback.core.FileAppender">
		<file>${LOG_HOME}/${LOG_FILE_NAME}</file>
		<encoder>
			<pattern>%date{ISO8601} %-5level %-5level %logger{36} %X{sourceThread} - %msg%n</pattern>
		</encoder>
	</appender>

	<root level="DEBUG">
		<appender-ref ref="STDOUT" />
		<appender-ref ref="FILE" />
	</root>
</configuration>

Hope this helps someone.

One thought on “ClassNotFoundException when adding slf4j to Akka

  1. It does, I’ve been trying to track this issue down for ages. This article you’ve wrote in 2014 was a god send. Thanks!

Leave a comment