Overview

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 http://scalatra.org/guides/2.6/monitoring/logging.html

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.

Details

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 http://scalatra.org/guides/2.6/monitoring/logging.html. This will let you generate the following kind of log data to your STDOUT:

07:58:38.004 [qtp1915910607-17] DEBUG o.b.honstain.app.ToyInventory - Creating inventory sku:ZL104
07:58:38.005 [qtp1915910607-17] DEBUG o.b.honstain.app.ToyInventory - 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 https://github.com/logstash/logstash-logback-encoder

<configuration>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>log/log-ToyInventoryApp.log</file>
    <encoder class="net.logstash.logback.encoder.LogstashEncoder">
      <customFields>{"application":"log-ToyInventoryApp"}</customFields>
    </encoder>
  </appender>

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

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":"org.bitbucket.honstain.app.ToyInventory","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":"org.bitbucket.honstain.app.ToyInventory","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 https://bitbucket.org/honstain/scalatra-example-blog/commits/99fa1f17980bee4c3d29374bb1bcc8e5d0887802

The documentation for logback is a good reference if you are unfamiliar with this type of logging framework https://logback.qos.ch/manual/architecture.html

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.