Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.serjic:klogging:1.1.0-1'
}
dependencies {
implementation("com.github.serjic:klogging:1.1.0-1")
}
<dependency>
<groupId>com.github.serjic</groupId>
<artifactId>klogging</artifactId>
<version>1.1.0-1</version>
</dependency>
libraryDependencies += "com.github.serjic" % "klogging" % "1.1.0-1"
:dependencies [[com.github.serjic/klogging "1.1.0-1"]]
KLogging provides unified logging API, which you can use from Kotlin code targeted for both JVM and Javascript. The library is inspired by
KLogger class features 5 levels of logging (to mirror that of SLF4J): trace, debug, info, warn, error with 2 flavors each:
logger.trace("This string will be evaluated regardless if trace enabled = ${logger.isTraceEnabled}")
logger.trace {"This string will not be evaluated unless trace enabled = ${logger.isTraceEnabled}"}
To obtain an instance of a logger you need to call one of the logger()
methods of KLoggers
(please note that JVM version of this class provides more overloads) or use mix it in:
class Foo {
val logger = KLoggers.logger(this)
fun test() {
logger.info("Have some logging!")
}
}
class Bar : WithLogging by KLoggerHolder() {
fun test() {
logger.info("Have some logging!")
}
}
class Baz {
companion object: WithLogging by KLoggerHolder()
fun test() {
logger.info("Have some logging!")
}
}
For file-level loggers I recommend following IntelliJ IDEA live template:
<template name="log" value="private val log = klogging.KLoggers.logger("$LOGGER_NAME$")" description="Logger" toReformat="false" toShortenFQNames="true">
<variable name="LOGGER_NAME" expression="groovyScript("com.intellij.openapi.module.ModuleUtil.findModuleForFile(_editor.virtualFile, _editor.project).name + \"/\" + _editor.virtualFile.name") " defaultValue="" alwaysStopAt="false" />
<context>
<option name="KOTLIN_TOPLEVEL" value="true" />
</context>
</template>