Lightweight logging framework for Kotlin. A convenient and performant logging library wrapping slf4j with Kotlin extensions.
Show your ❤️ with a ⭐
- Call log methods, without checking whether the respective log level is enabled:
logger.debug { "Some $expensive message!" }Behind the scenes the expensive message do not get evaluated if debug is not enabled:
if (logger.isDebugEnabled) logger.debug("Some $expensive message!")- Define the logger, without explicitly specifiying the class name:
val logger = KotlinLogging.logger {}or
companion object: KLogging()Behind the scenes val logger will be created in the class, with the class/file name:
val logger = LoggerFactory.getLogger("class name")- Log exceptions in a Kotlin-style
logger.error(exception) { "a $fancy message about the $exception" }val logger = KotlinLogging.logger {}
class FooWithLogging {
val message = "world"
fun bar() {
logger.info { "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 runtime, it is also required to depend on a logging implementation. More details here.
<dependency>
<groupId>io.github.microutils</groupId>
<artifactId>kotlin-logging</artifactId>
<version>1.4.1</version>
</dependency>See full example in kotlin-logging-example-maven.
compile 'io.github.microutils:kotlin-logging:1.4.1'Or alternatively, download jar from github or bintray or maven-central.
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.
- 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.
- See wiki for more examples.
- 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
- 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
- Pull requests are welcome!
