Scalatra Logging

Scala Feb 8, 2019


If you want to add logging to your Scalatra service and get some basic log strings coming to STDOUT, following the Scalatra documentation for logging is the best reference

The rest of this post is just the details of me adding additional logging to the example Scalata service from this series of blog posts.


The logging framework recommended by Scalatra is Logback, so if you follow the official getting started guide (or went through one of the previous posts on creating a Scalatra service), you would likely have the following dependency for Logback in your project.

"ch.qos.logback" % "logback-classic" % "1.2.3" % "runtime",

The Scalatra site also has an simple example that shows how to get basic support running This will let you generate the following kind of log data to your STDOUT:

07:58:38.004 [qtp1915910607-17] DEBUG - Creating inventory sku:ZL104
07:58:38.005 [qtp1915910607-17] DEBUG - Creating inventory Inventory(ZL104,3,Pine Block of Wood)

If you would also like to generate a log file with JSON (if you are interested in constructing some structured log data) the following project was helpful for me


  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <encoder class="net.logstash.logback.encoder.LogstashEncoder">

  <root level="debug">
    <appender-ref ref="FILE" />

The previous log message would then appear as the following in your log file:

{"@timestamp":"2019-02-08T07:58:38.004-08:00","@version":"1","message":"Creating inventory sku:ZL104","logger_name":"","thread_name":"qtp1915910607-17","level":"DEBUG","level_value":10000,"application":"log-ToyInventoryApp"}
{"@timestamp":"2019-02-08T07:58:38.005-08:00","@version":"1","message":"Creating inventory Inventory(ZL104,3,Pine Block of Wood)","logger_name":"","thread_name":"qtp1915910607-17","level":"DEBUG","level_value":10000,"application":"log-ToyInventoryApp"}

For additional reference you can look to the commit I made to this example Scalatra project

The documentation for logback is a good reference if you are unfamiliar with this type of logging framework

In summary, this is a reasonable start but we still have some way to go before we could optimally interface with an ELK or Splunk.