days
0
-18
0
hours
-1
-8
minutes
0
-4
seconds
-1
-2
search
Lightweight Kotlin logging

Kotlin-logging wraps SLF4J for better native Kotlin support

Sarah Schlothauer
kotlin
© Shutterstock / Krasula

Kotlin-logging is a lightweight logging framework written in pure Kotlin. It wraps SLF4J (Simple Logging Facade for Java) with additional Kotlin extensions. While we wait for the release of Kotlin 1.4, let’s take a quick look at this wrapper and how it expands upon plain SLF4J, and what alternatives exist for Android development.

As we wait for the release of Kotlin 1.4 this year, let’s take a look at a helpful open source framework and how it improves logging.

Kotlin-logging is a lightweight logging framework written in pure Kotlin and wraps SLF4J (Simple Logging Facade for Java) with additional Kotlin extensions. SLF4J provides a logging API and acts as a simple facade or abstraction for differing logging frameworks.

SEE ALSO: Java on VS Code February update adds new features for dependency management

Native support

Why use a wrapper and not just vanilla SLF4J? Namely, this framework has better native Kotlin support.

In addition, not only does it inherit support SLF4J methods and implementations, but it adds more functionality. With this framework, you can use call methods, without having to check if the respective log level is enabled.

Furthermore, you can define the logger without explicitly specifying the class name.

For example:

// Place definition above class declaration to make field static
private val logger = KotlinLogging.logger {}

According to the README:

After seeing many questions like Idiomatic way of logging in Kotlin and Best practices for loggers, it seems like there should be a standard for logging and obtaining a logger in Kotlin. kotlin-logging provides a wrapper for slf4j-api to be used by Kotlin classes with the following advantages:

  • No need to write the logger and class name or logger name boilerplate code.
  • A straight forward way to log messages with lazy-evaluated string using lambda expression {}.
  • All previous slf4j implementation can still be used.

“All the benefits like in Scala”

A Medium.com post discusses the creation of Kotlin-logging and how it solves issues that simple SLF4J presents. It removes boilerplate code, making the code much cleaner, and thus, more resilient against errors.

According to the post, using Kotlin-logger gives the language “all the benefits like in Scala”.

In a further blog, the author compares Kotlin-logging to AnkoLogger for Android developers. (Compare the two frameworks here.)

Getting started

View the wiki for more examples and instructions on how to install it. You can download Kotlin-logging with Maven, Gradle, or download the JAR from GitHub or Bintray. (The latest release is v1.7.8.)

SEE ALSO: Helidon 2.0.0-M1: Better microservices building with Java libraries

A sample Android project is available to browse on GitHub.

As for multiplatform support, currently, experimental common and JS support is available. For JS usage, Kotlin-logging supports all common interfaces and methods.

Since this framework depends on the SLF4J-API, you will need to follow the instructions for configuring SLF4J with a different logger implementation.

Author
Sarah Schlothauer

Sarah Schlothauer

All Posts by Sarah Schlothauer

Sarah Schlothauer is an assistant editor for JAXenter.com. She received her Bachelor's degree from Monmouth University and is currently enrolled at Goethe University in Frankfurt, Germany where she is working on her Masters. She lives in Frankfurt with her husband and cat. She is also the editor for Conditio Humana, an online magazine about ethics, AI, and technology.

Leave a Reply

avatar
400