Simple multiplatform logging in Kotlin
Go to file
InsanusMokrassar 24adeef67b
Merge pull request #67 from InsanusMokrassar/1.3.2
1.3.2
2024-01-17 23:33:45 +06:00
.github/workflows update github workflows java toolchain versions 2023-09-20 21:45:39 +06:00
gradle update gradle wrapper 2024-01-16 12:48:29 +06:00
kotlin-js-store update of gradle wrapper and publishing scripts 2023-09-20 21:39:01 +06:00
src hotfix of js assert accordance 2023-11-18 18:08:19 +06:00
.gitignore update of gradle wrapper and publishing scripts 2023-09-20 21:39:01 +06:00
CHANGELOG.md update dependencies 2024-01-16 12:47:07 +06:00
LICENSE Initial commit 2022-06-07 13:17:21 +04:00
README.md Update README.md 2023-06-10 18:16:35 +06:00
build.gradle fixes of publication? 2023-11-02 18:06:55 +06:00
changelog_parser.sh add publication workaround 2022-06-07 20:30:54 +06:00
defaultAndroidSettings.gradle update gradle versions and publication scripts 2023-11-02 17:59:22 +06:00
dokka.gradle dokka.gradle 2022-06-07 23:14:01 +06:00
github_release.gradle update dependencies 2024-01-16 12:47:07 +06:00
gradle.properties start 1.3.2 2024-01-16 12:46:07 +06:00
gradlew Initial commit 2022-06-07 13:17:21 +04:00
gradlew.bat Initial commit 2022-06-07 13:17:21 +04:00
publish.gradle update artifacts to be published in nexus 2023-12-05 19:41:15 +06:00
publish.kpsb update artifacts to be published in nexus 2023-12-05 19:41:15 +06:00
renovate.json Add renovate.json 2023-04-18 08:23:00 +00:00
settings.gradle update of gradle wrapper and publishing scripts 2023-09-20 21:39:01 +06:00

README.md

KSLog

It is simple and easy-to-use tool for logging on the most popular platforms in Kotlin Multiplatform:

JVM Android Js ARM x64 ARM x32 Linux x64

KDocs Tutorials

By default, KSLog is using built-in tools for logging on each supported platform:

  • java.util.logging.Logger for JVM
  • android.util.Log for Android
  • Console for JS

But you always may create your logger and customize as you wish:

KSLog.default = KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? ->
    // do your logging
}

This library also supports native targets in experimental mode. By default all native targets will use simple printing in the console

How to use

Fast-travel

Just use some boring extensions like:

KSLog.i("Some message")
// OR
KSLog.i("Some tag", "Some message")
// OR
KSLog.i("Some tag", "Some message", IllegalArgumentException("So, that is exception :)"))
// OR
KSLog.i("Some optional tag", Exception("Optional")) { "Lazy inited message" }
// OR
KSLog.iS("Some optional tag", Exception("Optional")) { "Lazy inited message for suspendable calculation of text" }
// OR EVEN
KSLog.l(LogLevel.INFO, "Some tag", "Some message", IllegalArgumentException("So, that is exception :)"))
// OR
KSLog.l(LogLevel.INFO, "Some optional tag", IllegalArgumentException("So, that is exception :)")) { "And lazily inited message" }

A little bit deeper

There are several important "terms" in context of this library:

  • Default logger (available via KSLog.default or simply KSLog)
  • Local logger (can be created via KSLog functions and passed anywhere as KSLog)
  • Logging shortcuts like KSLog.i/KSLog.info
  • Built-in extension Any.logger which allow you to create logger binded to the default with the tag based on the class of receiver
    • Be careful with the receivers: if you will use some extension like apply, the receiver will be different with your class inside of that apply

Every logging extension (like KSLog.i) have its analog with lazy inited message text and the same one with suffix S (like KSLog.iS) for the suspendable message calculation.

Default logger can be created by passing defaultTag and one of variants log level filters: set or minimal loggable level. In JVM you also may setup any logger as base logger for default realizations of KSLog. Besides, you may use your own callback (on any target platform) as output of logging:

val logger = KSLog { logLevel, optionalTag, message, optionalThrowable ->
    println("[$logLevel] $optionalTag - $message: $optionalThrowable.stackTraceToString()")
}

In the example above we will take the logger which will just print incoming data as common output.

Installation

Maven Central

Gradle

implementation "dev.inmo:kslog:$kslog_version"

Maven

<dependency>
  <groupId>dev.inmo</groupId>
  <artifactId>kslog</artifactId>
  <version>${kslog_version}</version>
</dependency>