What is a good way to do logging in a Scala application? Something that is consistent with the language philosophy, does not clutter the code, and is low-maintenance and unobtrusive. Here's a basic requirement list:
- simple
- does not clutter the code. Scala is great for its brevity. I don't want half of my code to be logging statements
- log format can be changed to fit the rest of my enterprise logs and monitoring software
- supports levels of logging (ie debug, trace, error)
- can log to disk as well as other destinations (i.e. socket, console, etc.)
- minimum configuration, if any
- works in containers (ie, web server)
- (optional, but nice to have) comes either as part of the language or as a maven artifact, so I don't have to hack my builds to use it
I know I can use the existing Java logging solutions, but they fail on at least two of the above, namely clutter and configuration.
Thanks for your replies.
Quick and easy forms.
Scala 2.10 and older:
And build.sbt:
Scala 2.11+ and newer:
And build.sbt:
Don't use Logula
I've actually followed the recommendation of Eugene and tried it and found out that it has a clumsy configuration and is subjected to bugs, which don't get fixed (such as this one). It doesn't look to be well maintained and it doesn't support Scala 2.10.
Use slf4s + slf4j-simple
Key benefits:
-Dorg.slf4j.simplelogger.defaultlog=trace
to execution command or hardcode in your script:System.setProperty("org.slf4j.simplelogger.defaultlog", "trace")
. No need to manage trashy config files!Run/Debug Configurations
and add-Dorg.slf4j.simplelogger.defaultlog=trace
toVM options
.Here's what you need to be running it with Maven:
I find very convenient using some kind of java logger, sl4j for example, with simple scala wrapper, which brings me such syntax
In my opinion very usefull mixin of java proven logging frameworks and scala's fancy syntax.
This is how I got Scala Logging working for me:
Put this in your
build.sbt
:Then, after doing an
sbt update
, this prints out a friendly log message:If you are using Play, you can of course simply
import play.api.Logger
for writing log messages:Logger.debug("Hi")
.See the docs for more info.
Writer
,Monoid
and aMonad
implementation.With Scala 2.10+ Consider ScalaLogging by Typesafe. Uses macros to deliver a very clean API
https://github.com/typesafehub/scala-logging
Quoting from their wiki:
After the macro has been applied, the code will have been transformed into the above described idiom.
In addition ScalaLogging offers the trait
Logging
which conveniently provides aLogger
instance initialized with the name of the class mixed into: