layout | title | section |
---|---|---|
home |
Home |
home |
This is a quick start with slog4s on the JVM with logback backend. It doesn't mean we are limited to JVM or logback! Au contraire!
Add new library dependencies to your build.sbt
libraryDependencies ++= Seq("com.avast" %% "slog4s-api" % "@VERSION@",
"com.avast" %% "slog4s-slf4j" % "@VERSION@")
We will be using logback with logstash encoder. So make sure it's in your dependency list and it's configured properly.
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder" />
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Obligatory imports before we start:
import cats.effect._
import cats.syntax.all._
import slog4s._
import slog4s.slf4j._
import slog4s.docs.Helpers
Helpers.init()
Let's start by creating a LoggerFactory
that is backed by slf4j (and logback under the hood). With that, we
can create a named Logger
.
val loggerFactory = Slf4jFactory[IO].withoutContext.loggerFactory
val logger = loggerFactory.make("test-logger")
It's finally time to log something!
logger.info("Hello world!").unsafeRunSync()
That was pretty boring, except file
and line
attributes that denote location of the log message within a file.
We can also provide an exception:
logger.error(new Exception("Boom!"), "Something went horribly wrong.").unsafeRunSync()
Let's make our message more content rich with additional arguments:
logger.info
.withArg("string_value", "<VALUE>")
.withArg("bool_value", true)
.withArg("list_value", List(1,2,3))
.log("Message with arguments")
.unsafeRunSync()
Additional arguments are type safe. Following code will fail in compile time:
class That(value: String)
logger.info
.withArg("that_value", new That("value"))
.log("Does not compile")
.unsafeRunSync()
However there is built-in support for case class
es and sealed trait
s provided by generic
module.