Lightweight logging framework for Kotlin, written in .
A convenient and performant logging library wrapping slf4j with Kotlin extensions.
logger.debug { "Some $expensive message!" }
Behind the scenes the expensive message do not get evaluated if debug is not enabled:
// This is what happens when you write the above ^^^
if (logger.isDebugEnabled) logger.debug("Some $expensive message!")
// Place definition above class declaration to make field static
private val logger = KotlinLogging.logger {}
Behind the scenes val logger
will be created in the class, with the class/file name:
// This is what happens when you write the above ^^^
val logger = LoggerFactory.getLogger("package.ClassName")
// exception as first parameter with message as lambda
logger.error(exception) { "a $fancy message about the $exception" }
private val logger = KotlinLogging.logger {}
class FooWithLogging {
val message = "world"
fun bar() {
logger.debug { "hello $message" }
}
}
An Android
example project with kotlin logging can be found in kotlin-logging-example-android.
Important note: kotlin-logging depends on slf4j-api (In the JVM artifact). In runtime, it is also required to depend on a logging implementation. More details in how-to-configure-slf4j. And an excelent detailed explanation in a-guide-to-logging-in-java.
Multiplatform: See the section below.
<dependency>
<groupId>io.github.microutils</groupId>
<artifactId>kotlin-logging</artifactId>
<version>1.6.24</version>
</dependency>
See full example in kotlin-logging-example-maven.
compile 'io.github.microutils:kotlin-logging:1.6.24'
Or alternatively, download jar from github or bintray or maven-central.
An experimental common & js support is available.
More info on wiki and issue #21.
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 provide 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 boileplates.
- A straight forward way to log messages with lazy-evaluated string using lambda expression
{}
. - All previous slf4j implementation still can be used.
- https://www.jetbrains.com/youtrack/ (https://www.jetbrains.com/help/youtrack/standalone/Third-Party-Software-Used-by-YouTrack.html)
- https://github.com/DiUS/pact-jvm
- https://github.com/AsynkronIT/protoactor-kotlin
- https://github.com/square/misk
- https://github.com/openrndr/openrndr
- https://github.com/JetBrains/xodus
And many more... (add your name above)
- Why not use plain slf4j? kotlin-logging has better native kotlin support. It adds more functionality and enable less boilerplates.
- Is all slf4j implementation supported (Markers, params, etc')? Yes, KLogger inherits Logger and all methods are supported.
- Is location logging possible? Yes, location awerness was added in kotlin-logging 1.4.
- When I do
logger.debug
, my IntelliJ IDEA run console doesn't show any output. Do you know how I could set the console logger to debug or trace levels? Is this an IDE setting, or can it be set in the call to KLogging()? Setting log level is done per implementation. Kotlin-logging and slf4j are just facades for the underlying logging lib (log4j, logback etc') more details here. - Can I access the actual logger? Yes, via
KLogger.underlyingLogger
property.
- See wiki for more examples.
It is possible to configure Intellij live template. For file level logger configure the following:
- Text template:
private val logger = mu.KotlinLogging.logger {}
. - Applicable in
Kotlin: top-level
.
- Open an issue here: https://github.com/MicroUtils/kotlin-logging/issues
- Ask a question in StackOverflow with kotlin-logging tag.
- Chat on Slack channel: https://kotlinlang.slack.com/messages/kotlin-logging
- Chat on Telegram channel: https://t.me/klogging
- kotlin-logging is hosted on bintray and maven central.
- https://medium.com/@OhadShai/logging-in-kotlin-95a4e76388f2
- kotlin-logging vs AnkoLogger for Android
- How kotlin-logging was published
- Kotlin Logging Without the Fuss
- DropWizard + Kotlin = Project Kronslott
- Logging in Kotlin – the right approach
- https://bednarek.wroclaw.pl/log4j-in-kotlin/
- Ohad Shai
- Pavel Nikitin [JetBrains]
- Christoph Gritschenberger
- Igor Manushin
- krokofant
- ravikumar-n
- Dario Seidl
- b14ckyfox
- robertfmurdock
- Corneil du Plessis
See also here: https://github.com/MicroUtils/kotlin-logging/graphs/contributors
Pull requests are welcome!
Show your ❤ with a ★