Adding simple logging to scala project
Recently i wanted to add some simple logging to tests in my scala project so it would be easier to understand with wich generated values i'm facing in each test. After some trials i came up to this nice and easy solution
To set it up you just need to add next to your build.sbt file :
"com.typesafe.scala-logging" %% "scala-logging-slf4j" % "2.1.2",
"com.typesafe" %% "scalalogging-slf4j" % "1.0.1",
"org.slf4j" % "slf4j-api" % "1.7.1",
"org.slf4j" % "log4j-over-slf4j" % "1.7.1", // for any java classes looking for this
"ch.qos.logback" % "logback-classic" % "1.0.3"
After that there's 2 ways how you can use logging in your classes :
import com.typesafe.scalalogging.slf4j.LazyLogging
class Foo extends LazyLogging {
logger.info("This is information log message ")
logger.warning("This is warning log message ")
logger.debug("This is debug log message")
logger.error("This is error log message")
}
or you can :
import com.typesafe.scalalogging.slf4j.LazyLogging
class Foo extends Bar with LazyLogging {
// your logging here
}
The result of this messages will look like this :
[main] INFO Foo.FooSuite - This is info log message
[main] WARN Foo.FooSuite - This is warning log message
[main] DEBUG Foo.FooSuite - This is debug log message
[main] ERROR Foo.FooSuite - This is error log message