-
-
Notifications
You must be signed in to change notification settings - Fork 126
Home
Welcome to the kotlin-logging wiki!
Add the below dependency to start using kotlin-logging.
<dependency>
<groupId>microutils</groupId>
<artifactId>kotlin.logging</artifactId>
<version>1.2</version>
</dependency>
compile 'microutils:kotlin.logging:1.2'
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()
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"}
}
}