ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - oshai/kotlin-logging: Lightweight Multiplatform logging framework for Kotlin. A convenient and performant logging facade.
Lightweight Multiplatform logging framework for Kotlin. A convenient and performant logging facade. - oshai/kotlin-logging
Visit Site

GitHub - oshai/kotlin-logging: Lightweight Multiplatform logging framework for Kotlin. A convenient and performant logging facade.

GitHub - oshai/kotlin-logging: Lightweight Multiplatform logging framework for Kotlin. A convenient and performant logging facade.

kotlin-logging CI Slack channel Maven Central Apache License V.2

Lightweight Multiplatform logging framework for Kotlin, written in Pure Kotlin.
A convenient and performant logging facade.

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:

// This is what happens when you write the above ^^^
if (logger.isDebugEnabled) logger.debug("Some $expensive message!")

Define the logger, without explicitly specifying the class name

// Place definition above class declaration to make field static
private val logger = KotlinLogging.logger {}

Behind the scenes val logger will be created in the class, with the class/file name:

// This is what happens when you write the above ^^^
val logger = LoggerFactory.getLogger("package.ClassName")

Log exceptions in a Kotlin-style

// exception as first parameter with message as lambda
logger.error(exception) { "a $fancy message about the $exception" }

Fluent logging in a Kotlin-style

logger.atWarn {
    message    = "foo $bar"
    cause      = exception
    payload    = buildMap(capacity = 3) {
        put("foo", 1)
        put("bar", "x")
        put("obj", Pair(2, 3))
    }
}

Getting started

import io.github.oshai.kotlinlogging.KotlinLogging

private val logger = KotlinLogging.logger {} 

class FooWithLogging {
    val message = "world"
    fun bar() {
        logger.debug { "hello $message" }
    }
}

Version 5 vs. previous versions

Version 5 is not backward compatible with previous versions (v.3, v.2, v.1). Group id (in maven) and packages names changed. It is possible to use both version 5 and previous versions side-by-side so some of the code from the old version and some new. It is also possible to have libs using old version and use the new version (and vice-versa).
In that sense it's a completely new dependency.

Main changes are:

  • Maven group id changed from io.github.microutils -> io.github.oshai.
  • Root package change from mu -> io.github.oshai.kotlinlogging.
  • Slf4j dependency is not provided anymore (users have to provide it). It means that >= 5.x can work with both slf4j 1 or 2.
  • There are changes to multiplatform class hierarchy that might break compatibility.

More details in issue #264, and in the change log

Download

Important note: kotlin-logging depends on slf4j-api (in the JVM artifact). In runtime, it is also required to depend on a logging implementation. More details in how-to-configure-slf4j. And an excellent detailed explanation in a-guide-to-logging-in-java.
In version 5 users should also provide slf4j-api dependency.

In short, if you just want to log statements to stdout, it's possible to add the following dependency: org.slf4j:slf4j-simple:2.0.3.

Maven

<dependency>
  <groupId>io.github.oshai</groupId>
  <artifactId>kotlin-logging-jvm</artifactId>
  <version>7.0.0</version>
</dependency>

See the full example in kotlin-logging-example-maven.

Gradle

implementation 'io.github.oshai:kotlin-logging-jvm:7.0.0'

Alternatively, download the JAR from github or maven-central.

Multiplatform

An experimental multiplatform support is available.
More information is available on the wiki and issues #21 #45.

Overview

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 provides 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 boilerplate code.
  • A straight forward way to log messages with lazy-evaluated string using lambda expression {}.
  • All previous slf4j implementation can still be used.

Who is using it

And many more... (add your name above)

FAQ

  • Why not use plain slf4j? kotlin-logging has better native Kotlin support. It adds more functionality and enables less boilerplate code.
  • Is all slf4j implementation supported (Markers, params, etc')? Yes.
  • Is location logging possible? Yes, location awareness was added in kotlin-logging 1.4.
  • When I do logger.debug, my IntelliJ IDEA run console doesn't show any output. Do you know how I could set the console logger to debug or trace levels? Is this an IDE setting, or can it be set in the call to KLogging()? Setting log level is done per implementation. kotlin-logging and slf4j are just facades for the underlying logging lib (log4j, logback etc') more details here.
  • Can I access the actual logger? In platforms available yes, via DelegatingKLogger.underlyingLogger property.

Usage

  • See wiki for more examples.

It is possible to configure IntelliJ live templates. For file level logger configure the following:

  • Text template: private val logger = io.github.oshai.kotlinlogging.KotlinLogging.logger {}.
  • Applicable in Kotlin: top-level.

Support

More links

Contributing

Any contribution is appreciated.
See the contributors list in: https://github.com/oshai/kotlin-logging/graphs/contributors

Pull requests are welcome! See instructions in https://github.com/oshai/kotlin-logging/blob/master/CONTRIBUTING.md.

Show your ❤ with a ★

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory