Skip to content
This repository was archived by the owner on May 1, 2023. It is now read-only.
/ klogger Public archive

A Kotlin library for logging which extends slf4j.

License

Notifications You must be signed in to change notification settings

rs3vans/klogger

Repository files navigation

klogger Travis Bintray

A Kotlin library for logging.

Overview

This library is a simple set of extensions around the popular slf4j logging API which aim to make using them easier when writing in Kotlin.

Motivation

The popular slf4j framework is used in many existing libraries. It makes sense to extend this library for use in Kotlin, rather than creating yet another logging library from scratch.

Using

Obtaining a logger

Nothing prevents users of klogger from obtaining a Logger by the usual means, but the following extended ways are possible with klogger:

// obtain a logger for any String using it as the logger's name
val log = "com.rs3vans.kt.klogger".logger()

// obtain a logger for any type, using the class name as the logger's name
val log = logger<SomeType>()

// obtain a logger _inside_ any type (e.g. for use within a class)
class Thing {
    private val log = logger()
    
    fun doThing() {
        log.trace("A message")
    }
}

// ...or by a lazy delegate
class Thing {
    private val log by lazyLogger()
    
    fun doThing() {
        log.trace("A message")
    }
}

Logging messages and exceptions

Again, all existing methods of Logger are free to be used, but the following extensions make logging with klogger a little nicer:

// log a message using a lambda that will only be evaluated if the given level is enabled
log.trace { "Assembling this message is resource-intensive: ${loadProperties()}" }

// the same form can accept throwable causes (`warn` and `error` only)
log.error(cause) { "Assembling this message is resource-intensive: ${loadProperties()}" }

Logging within an object or class

A fairly common pattern is to create an internal static logger for each class that will be logging messages or errors. klogger provides an interface for adding this behavior to an object or class, and then providing easy access to the various logging functions without referencing the logger with each invocation:

class Thing : Logging {
    override val logger = logger()
    
    fun doThing() {
        trace("A message")
    }
}

Additionally, this logging behavior can be added to a class or companion object via entension of the WithLogging type:

class Thing {
    fun doThing() {
        trace("A message")
    }
    
    companion object : WithLogging()
}

Download

klogger binaries can be downloaded from Bintray.

Gradle

You can add klogger to your project using Gradle by adding the following to your build.gradle dependencies:

compile 'com.github.rs3vans.klogger:klogger:1.0.1'

Maven

You can add klogger to your project using Gradle by adding the following to your build.gradle dependencies:

<dependency>
  <groupId>com.github.rs3vans.klogger</groupId>
  <artifactId>klogger</artifactId>
  <version>1.0.1</version>
</dependency>

License

klogger is licensed under the Apache Software License v2.0.

slf4j is licensed under the MIT License.

About

A Kotlin library for logging which extends slf4j.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages