QuickKnol: Integrating Logback with Scala


Logback is the successor of Log4j and is one of the better logging frameworks around. In this post, we would quickly see how to set it up on a Scala project and how to start using it.

The relevant dependency to be included in your build.sbt is

libraryDependencies += “ch.qos.logback” % “logback-classic” % “1.0.1”

Once, you have this set, next you have to get a logback.xml file in your classpath. If you are using a sbt generated project, the best place to put it would be /src/main/resources and for your tests, you could put a logback-test.xml in /src/test/resources

A sample, logback.xml file would look like this

<configuration>
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>%d{HH:mm:ss.SSS} TKD [%thread] %-5level %logger{36} - %msg%n
			</pattern>
		</encoder>
	</appender>

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

Once these are done, next let us just test it out. Following is a small Scala program to check

package com.knoldus.logme
import org.slf4j.LoggerFactory
import ch.qos.logback.core.util.StatusPrinter
import ch.qos.logback.classic.LoggerContext

object LogTester extends App{
  def logger = LoggerFactory.getLogger("KDURLFilter")
  StatusPrinter.print((LoggerFactory.getILoggerFactory).asInstanceOf[LoggerContext])
  logger.info("vikas")
}

and the output

13:05:43,469 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
13:05:43,470 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
13:05:43,470 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/home/vikas/dev/12kd/LogbackWAssembly/bin/logback.xml]
13:05:43,789 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
13:05:43,810 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
13:05:43,818 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
13:05:43,975 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
13:05:44,048 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
13:05:44,048 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
13:05:44,049 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
13:05:44,051 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@1e6db24 - Registering current configuration as safe fallback point

13:05:44.057 TKD [main] INFO  KDURLFilter - vikas

Entire code for this sample is present on the Knoldus github site

Happy Logging.

About Vikas Hazrati

Vikas is the Founding Partner @ Knoldus which is a group of software industry veterans who have joined hands to add value to the art of software development. Knoldus does niche Reactive and Big Data product development on Scala, Spark and Functional Java. Knoldus has a strong focus on software craftsmanship which ensures high-quality software development. It partners with the best in the industry like Lightbend (Scala Ecosystem), Databricks (Spark Ecosystem), Confluent (Kafka) and Datastax (Cassandra). To know more, send a mail to hello@knoldus.com or visit www.knoldus.com
This entry was posted in Scala and tagged , . Bookmark the permalink.

One Response to QuickKnol: Integrating Logback with Scala

  1. Hi, good post. But I think you still need as dependencies:

    “org.slf4j” % “slf4j-api” % “1.7.1”,
    “ch.qos.logback” % “logback-core” % “1.0.7”,
    “ch.qos.logback” % “logback-classic” % “1.0.7”,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s