Skip to content

Multiplatform support

Ohad Shai edited this page Jan 8, 2025 · 16 revisions

Modules

As of version 2.0 kotlin-logging has multiplatfom support. It composed of various modules for the specific platform:

  • Common - artifact id: kotlin-logging
  • JVM - artifact id: kotlin-logging-jvm
  • JS - artifact id: kotlin-logging-js
  • Linux - artifact id: kotlin-logging-linuxx64

Full list of modules can be found in: https://repo1.maven.org/maven2/io/github/oshai/

In order to include in gradle build file add the following dependency:

compile 'io.github.oshai:<artifact id>:<version>'
  • First supported version is 5.0.0 (for the new artifacts).
  • It is sufficient to add the dependency on the common module.

Common usage

Obtaining a logger is supported only by a field (no inheritance at the moment):

private val logger = KotlinLogging.logger {}

KLogger log messages support only lazy methods messages such as:

logger.info { "Hello World!" }

JS usage

All common interfaces and methods are supported.
In addition, in order to define log level set field KotlinLoggingConfiguration.logLevel during startup.
For example:

KotlinLoggingConfiguration.logLevel = Level.DEBUG
  • Default level is INFO

In JS there are many settings (browsers, node) that works a bit differently for name resolution. It is also possible to configure logger name like this (since version 5):

class MyClass {
  private val logger by KotlinLogging.logger()
}

Linux usage

All common interfaces and methods are supported.
In addition, in order to define log level set field KotlinLoggingConfiguration.logLevel during startup.
For example:

KotlinLoggingConfiguration.logLevel = Level.DEBUG
  • Default level is INFO

Android Usage

For Android only projects there are few options:

  • use Android module: io.github.oshai:kotlin-logging-android.
  • use the regular jvm artifact that redirect by default to slf4j (less recommended option).

Note: Multiplatform projects will use the Android module by default (when multiplatform is configured).

In kotlin-logging-android the current default is using slf4j. To use native android logging configure the following in main activity:

import io.github.oshai.kotlinlogging.KotlinLogging

// this part should be configured only once in the app to use native android logging
object Static {
    init {
        System.setProperty("kotlin-logging-to-android-native", "true")
    }
}
private val static = Static

// this should be configured in every class that uses logging
private val logger = KotlinLogging.logger {}

// then later in the code
logger.info { "This is logging of - kotlin-logging" }

slf4j-android is deprecated and isn't published with slf4j 2. Instead it's possible to use: https://github.com/nomis/slf4j-android. Still, native android Log is recommended and might be the default in the future releases of kotlin-logging.

Some more details: https://github.com/oshai/kotlin-logging/issues/359#issue-1891057213

Example project: https://github.com/MicroUtils/kotlin-logging-example-android