Giter Club home page Giter Club logo

kotlin-logging's Introduction

Lightweight logging framework for Kotlin, written in Pure Kotlin.
A convenient and performant logging library wrapping slf4j with Kotlin extensions.

  • 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:

if (logger.isDebugEnabled) logger.debug("Some $expensive message!")
  • Define the logger, without explicitly specifiying the class name:
// Place definition above class declaration to make field static
private val logger = KotlinLogging.logger {}

or

companion object: KLogging()

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

val logger = LoggerFactory.getLogger("class name")
  • Log exceptions in a Kotlin-style
logger.error(exception) { "a $fancy message about the $exception" }

Getting started

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

An Android example project with kotlin logging can be found in kotlin-logging-example-android.

Download

Important note: kotlin-logging depends on slf4j-api. In runtime, it is also required to depend on a logging implementation. More details here.

Maven

<dependency>
  <groupId>io.github.microutils</groupId>
  <artifactId>kotlin-logging</artifactId>
  <version>1.5.4</version>
</dependency>

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

Gradle

compile 'io.github.microutils:kotlin-logging:1.5.4'

Or alternatively, download jar from github or bintray or maven-central.

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

FAQ

  • Why not use plain slf4j? kotlin-logging has better native kotlin support. It adds more functionality and enable less boilerplates.
  • Is all slf4j implementation supported (Markers, params, etc')? Yes, KLogger inherits Logger and all methods are supported.
  • Is location logging possible? Yes, location awerness 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? Yes, via KLogger.underlyingLogger property.

Usage

  • See wiki for more examples.

Support

More links

Contributors

  • Ohad Shai
  • Pavel Nikitin [JetBrains]
  • Christoph Gritschenberger
  • Igor Manushin
  • krokofant
  • ravikumar-n
  • Dario Seidl

Contributing

Pull requests are welcome!
Show your โค with a โ˜…

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.