All Projects → michaelbull → Kotlin Inline Logger

michaelbull / Kotlin Inline Logger

Licence: isc
A logger facilitating lazily-evaluated log calls via Kotlin's inline classes & functions.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Kotlin Inline Logger

liquibase-slf4j
Liquibase SLF4J Logger.
Stars: ✭ 42 (-45.45%)
Mutual labels:  slf4j, jvm, logger
jcabi-log
Static Wrapper of SLF4J easing you from the necessity to create static LOGGER instances in each Java class
Stars: ✭ 53 (-31.17%)
Mutual labels:  slf4j, logger
herald
Log annotation for logging frameworks
Stars: ✭ 71 (-7.79%)
Mutual labels:  slf4j, logger
Multiplatform-Log
Kotlin Multi Platform Logger, for android an ios : Logcat & print
Stars: ✭ 49 (-36.36%)
Mutual labels:  logger, multiplatform
Kotlin Logging
Lightweight logging framework for Kotlin. A convenient and performant logging library wrapping slf4j with Kotlin extensions
Stars: ✭ 1,378 (+1689.61%)
Mutual labels:  jvm, slf4j
KmLogging
Kotlin multiplatform logging. High performance, composable and simple to use.
Stars: ✭ 21 (-72.73%)
Mutual labels:  jvm, logger
Tinylog
tinylog is a lightweight logging framework for Java, Kotlin, Scala, and Android
Stars: ✭ 360 (+367.53%)
Mutual labels:  logger, slf4j
Zircon
Zircon is an extensible and user-friendly, multiplatform tile engine.
Stars: ✭ 552 (+616.88%)
Mutual labels:  multiplatform, jvm
Mvikotlin
Extendable MVI framework for Kotlin Multiplatform with powerful debugging tools (logging and time travel)
Stars: ✭ 483 (+527.27%)
Mutual labels:  multiplatform, jvm
Izumi
Productivity-oriented collection of lightweight fancy stuff for Scala toolchain
Stars: ✭ 423 (+449.35%)
Mutual labels:  logger, slf4j
Kotlinmultiplatform
Kotlin MultiPlatform App (Android, iOS, JVM & JS). MVVM/MVP - Kotlin MultiPlatform
Stars: ✭ 661 (+758.44%)
Mutual labels:  multiplatform, jvm
Klock
Multiplatform Date and time library for Kotlin
Stars: ✭ 569 (+638.96%)
Mutual labels:  multiplatform, jvm
Scala Logging
Convenient and performant logging library for Scala wrapping SLF4J.
Stars: ✭ 804 (+944.16%)
Mutual labels:  jvm, slf4j
Logger logstash backend
Logstash backend for the Elixir Logger
Stars: ✭ 67 (-12.99%)
Mutual labels:  logger
Javacore
Java程序员所需要掌握的核心知识: 集合框架&JVM机制&多线程与并发框架&网络协议&Spring&Dubbo&MySQL&微服务等;希望胖友小手一抖,右上角来个 Star,感恩 1024
Stars: ✭ 73 (-5.19%)
Mutual labels:  jvm
Byte Buddy Tutorial
“Byte Buddy Tutorial” 中文翻译:Byte Buddy 教程。
Stars: ✭ 67 (-12.99%)
Mutual labels:  jvm
Signale
Highly configurable logging utility
Stars: ✭ 8,575 (+11036.36%)
Mutual labels:  logger
Loguru
Python logging made (stupidly) simple
Stars: ✭ 10,510 (+13549.35%)
Mutual labels:  logger
Hobbyscript
Yet Another JVM/LLVM Dynamic Language (LLVM Backend WIP)
Stars: ✭ 72 (-6.49%)
Mutual labels:  jvm
Capsule
Dead-Simple Packaging and Deployment for JVM Apps
Stars: ✭ 1,143 (+1384.42%)
Mutual labels:  jvm

kotlin-inline-logger

Maven Central CI Status License

A logger facilitating lazily-evaluated log calls via Kotlin's inline classes & functions.

Installation

repositories {
    mavenCentral()
}

dependencies {
    implementation("com.michael-bull.kotlin-inline-logger:kotlin-inline-logger:1.0.3")
}

Introduction

kotlin-inline-logger has been structured as a multiplatform project. Currently the only implementation supports the JVM, utilising SLF4J, however in future other platforms can also be supported by implementing the necessary platform-specific APIs found in src/commonMain.

If you would like to implement support for a platform, please don't hesitate to open a pull request on GitHub.

JVM

On the JVM, kotlin-inline-logger provides inline functions that delegate to SLF4J. Users migrating from an existing SLF4J solution will find existing calls to logging functions like logger.info("Example") now marked as @Deprecated, prompting them to replace their existing code with the lazily-evaluated alternatives.

ReplaceWith example

Creating loggers

By default, passing no argument when creating an InlineLogger() will utilise MethodHandles.lookup(), introduced in JDK7, to derive the name of the logger from the class that created it. This is described in SLF4J's documentation as an idiomatic method of declaring a logger that is resistant to cut-and-paste errors:

class TransactionExecutor {
    val transactionLogger = InlineLogger() // named com.package.TransactionExecutor
}

Named loggers can be created by passing a String:

val transactionLogger = InlineLogger("DatabaseTransactions")

Class loggers can be created by passing a ::class reference:

class TransactionExecutor

val transactionLogger = InlineLogger(TransactionExecutor::class)

Effectiveness

The code examples below illustrate how the Kotlin you write is compiled to interoperable Java, demonstrating how the expensiveCalculation() function is only invoked and interpolated with the log message if warn-level logging is enabled.

This allows you to replace usages of SLF4J's MessageFormatter, with the more idiomatic string templates Kotlin provides.

Kotlin:
import com.github.michaelbull.logging.InlineLogger

val logger = InlineLogger("CalculationLogger")

fun expensiveCalculation(): Int {
    return 1234 * 5678
}

fun main() {
    logger.warn { "The result is: ${expensiveCalculation()}" }
}
Decompiled Java:
import com.github.michaelbull.logging.InlineLogger;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class ExampleKt {
    @NotNull
    private static final Logger logger;

    @NotNull
    public static Logger getLogger() {
        return logger;
    }

    public static int expensiveCalculation() {
        return 1234 * 5678;
    }

    public static void main() {
        Logger $this$iv = logger;
        if (InlineLogger.isWarnEnabled-impl((Logger) $this$iv)) {
            String string = "The result is: " + ExampleKt.expensiveCalculation();
            $this$iv.warn(String.valueOf(string));
        }
    }

    public static /* synthetic */ void main(String[] arrstring) {
        ExampleKt.main();
    }

    static {
        Logger delegate$iv = LoggerFactory.getLogger("CalculationLogger");
        logger = InlineLogger.constructor-impl((Logger) delegate$iv);
    }
}

Contributing

Bug reports and pull requests are welcome on GitHub.

License

This project is available under the terms of the ISC license. See the LICENSE file for the copyright information and licensing terms.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].