Git Product home page Git Product logo

sklog's Introduction

sklog - simple Kotlin logging

Kotlin (JVM) logging library for printing colorized text to the console, with an easy upgrade path to the popular kotlin-logging library

Rationale

Logging is a necessary part of most projects, but setting it up can be a chore.

Java has several popular logging frameworks, as well as the built-in java.util.logging, but all come with their own issues.

For Kotlin, the kotlin-logging library has become relatively popular, but it's not a standalone logger. Instead, it acts as a wrapper around SLF4J, which is itself a logging "facade" (i.e., frontend), meaning that it requires a separate backend/implementation to work.

As a result, even if all you want to do is print log output to the console (System.err), you'll need to pull in three separate dependencies: kotlin-logging, SLF4J, and SLF4J's Simple implementation. That can be a lot, especially for smaller projects with relatively simple logging needs.

It can also be difficult to figure out how to configure these libraries, for example if you want to change the log level so that you actually see debug messages.

This library exists to make it easy to start logging, without worrying about dependencies and complex configuration.

Features

  • No dependencies, other than the Kotlin language and the Java standard library
  • Messages are printed to System.err
  • Output includes timestamp and the name of the class where the logger was declared
    • Possible to set custom logger names
  • Output colorized based on log level
    • But this can be disabled
  • Log level can be customized
  • No other customizations are available in this library
  • But it's really easy to upgrade to a more full-featured solution, the kotlin-logging library! (see below)

Example & usage

screenshot showing sample output of sklog

This screenshot was generated by the following program:

import sklog.KotlinLogging
import sklog.LogLevel

private val logger = KotlinLogging.logger {}

fun main() {
    // The default log level is DEBUG,
    // but it can be set to TRACE globally or on a per-logger basis
    logger.logLevel = LogLevel.TRACE

    println("Welcome! This is a test run of sklog, the simple kotlin logging library.")
    println("Each of the following lines is logger output.")

    logger.trace { "Trace" }
    logger.debug { "Debug" }
    logger.info { "Info" }
    logger.warning("Warning")
    logger.error("Error")
}

Using this library

This library is made available via JitPack. Here's how you can add it to your project:

Gradle (build.gradle.kts)

repositories {
    maven {
        url = uri("https://jitpack.io")

        // You can ensure that only this library is loaded from JitPack, and nothing else, using "repository content filtering": https://docs.gradle.org/current/userguide/declaring_repositories.html#sec:repository-content-filtering
        content {
            includeGroup("com.github.nmalkin")
        }
    }
}

dependencies {
    implementation("com.github.nmalkin:sklog:0.0.2")
}

Gradle (build.gradle)

repositories {
    maven {
        url 'https://jitpack.io'

        // You can ensure that only this library is loaded from JitPack, and nothing else, using "repository content filtering": https://docs.gradle.org/current/userguide/declaring_repositories.html#sec:repository-content-filtering
        content {
            includeGroup 'com.github.nmalkin'
        }
    }
}

dependencies {
    implementation 'com.github.nmalkin:sklog:0.0.2'
}

Maven

<repositories>
  <repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
  </repository>
</repositories>

<dependency>
  <groupId>com.github.nmalkin</groupId>
  <artifactId>sklog</artifactId>
  <version>0.0.2</version>
</dependency>

Upgrade path

If you start using this library and realize you need more features, upgrading to the kotlin-logging library should be very easy! This library's API was based on kotlin-logging and designed to be compatible. In particular:

  • Loggers are declared the same way
    private val logger = KotlinLogging.logger {}
  • Messages can be logged as lambdas (so they don't get evaluated if the log level is disabled)
    logger.debug { "Some $expensive message!" }

Because of this, upgrading to kotlin-logging might be as simple as swapping our your imports:

< import sklog.KotlinLogging
---
> import mu.KotlinLogging

sklog's People

Contributors

nmalkin avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.