-
-
Notifications
You must be signed in to change notification settings - Fork 112
Home
Welcome to the kotlin-logging wiki!
kotlin-logging is a lightweight logging framework for Kotlin. A convenient and performant logging library wrapping slf4j with Kotlin extensions kotlin-logging provides both:
- Idiomatic way to obtain a logger without the need to specify class name
- Enhanced logging api that integrates fluently with Kotlin
Add the below dependency to start using kotlin-logging.
<dependency>
<groupId>io.github.microutils</groupId>
<artifactId>kotlin-logging</artifactId>
<version>1.4</version>
</dependency>
compile 'io.github.microutils:kotlin-logging:1.4'
Important note: kotlin-logging depends on slf4j-api, but it is also required to depend on a logging implementation in runtime. More details here.
class FooWithLogging {
companion object: KLogging()
val message = "world"
fun bar() {
logger.info{"hello $message"}
}
}
The recommended usage is to have the Companion
object extends KLogging()
and using the logger
member in the class like that:
companion object: KLogging()
Then using the logger
:
logger.info("hello message")
For sequences that are expected to be frequently used prefer lazy evaluated messages:
logger.debug{"lazy evaluated $hello message"}
(String is inside a method and gets evaluated only if debug log level is enabled at runtime)
In cases the Companion
object already extending other class it is recommend to implement the KLoggable
interface:
companion object: Any(), KLoggable {
override val logger = logger()
...
}
Other (less recommended) alternatives are:
companion object: Any(), KLoggable by NamedKLogging("com.MyClass")
Or implementing it as a non static member:
class ClassHasLogging: KLoggable {
override val logger = logger()
fun test() {
logger.info{"hello message"}
}
}