Getting started with Kodein-Log

This guide assumes a JVM target. Kodein-Log works exactly the same on all targets that are supported by Kotlin (JVM, Android, JS, Native).

Install

In your build.gradle.kts file, add the Maven Central repository:

buildscript {
    repositories {
        mavenCentral()
    }
}

Then add the dependency:

dependencies {
    implementation 'org.kodein.log:kodein-log:0.10.1'
}

More detailed installation guide here.

Create and use a Logger

Before being able to log anything, you need to create a LoggerFactory, to define the format of your logs and on which frontend they should be sent.

  1. create a LoggerFactory

    val loggerFactory = LoggerFactory.default (1)
    1 Initialize a LoggerFactory with the default output format.
  2. create a Logger

    class MyController {
        private val logger = LoggerFactory.default.newLogger<MyController>() (1)
    }
    1 Initialize a Logger from a given LoggerFactory.

    Thanks to the awesome Kotlin type system we can even go further, without explicitly naming the Tag used for logging:

    class MyController(private val loggerFactory: LoggerFactory) {
        private val logger = newLogger(loggerFactory) (1)
    }
    1 Initialize a Logger from a given LoggerFactory, implicitly tagged with MyController.
  3. Use the logger

    logger.info { "Well done..." }
    logger.warning { "...you have completed the Getting Started guide." }
    Logging levels available: debug / info / warning / error.

More detailed on Logger initialization and usage.

If you want to know more about frontend implementation you can read Working with custom frontends.