sergejsha / logger

Minimalistic, fast and configurable Logger for Kotlin Multiplatform.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Maven Central maintenance-status

🪵 Logger

Minimalistic and concise Logger for Kotlin Multiplatform from one of my projects, shared with the awesome Kotlin community.

Architecture diagram

Usage

// Initialize
class App : Application() {
    override fun onCreate() {
        initializeLogger {
            registerPrintlnSink() // used by default if no sinks are configured
            registerMemoryRingSink()
            registerAndroidLogSink()
            loggableLevel = if (isDebugBuild) Everything else InfoAndSevere
        }
    }
}

// Use logger
private const val TAG = "Reader"

class Reader(private val bufferSize: Int) {

    init {
        i(TAG) { "created: size=$bufferSize" } // info
        d(TAG) { "created: size=$bufferSize" } // debug
    }

    suspend fun readData() {
        try {
            httpClient.post("read-data")
        } catch (e: Exception) {
            w(TAG, e) { "data reading failed" } // warning
            e(TAG, e) // error
        }
    }
}

iOS usage

init() {
    LogKt.initializeLogger { builder in
        builder.registerIosLogSink(logPrinter: LogPrinterCompanion.shared.Default)
    }
}

private let TAG = "SampleApp"
LogKt.d(tag: "\(TAG)Debug") { "debug message" }

iOS log

Android usage

override fun onCreate() {
    initializeLogger {
        registerAndroidLogSink()
    }
}

Android log

Desktop usage

fun main() {
    initializeLogger {
        registerPrintlnSink()
    }
}

Desktop log

Browser usage

fun main() {
    initializeLogger {
        registerConsoleLogSink()
    }
}

Browser log

How to

Disable logger for a class

// Create a named tag instance instead of a string-tag
private val tag = namedTag("ScaleAnimation")

// Use the tag in log-functions
d(tag) { "animation started" }
d(tag) { "scale: ${scale.value}" }
...

// Set a loggable level to disable unwanted logs for this tag
private val tag = namedTag("ScaleAnimation", LoggableLevel.Nothing)

Use class name for tags

class Reader {
    private val tag = classTag<Reader>()

    init {
        d(tag) { "Reader is created" }
    }
}

Dependencies

In gradle/libs.versions.toml

[versions]
kotlin = "2.0.0"
logger = "0.6"

[libraries]
logger = { module = "de.halfbit:logger", version.ref = "logger" }

[plugins]
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }

In shared/build.gradle.kts

plugins {
    alias(libs.plugins.kotlin.multiplatform)
}

kotlin {
    sourceSets {
        commonMain.dependencies {
            implementation(libs.logger)
        }
    }
}

Publishing

  1. Bump version in root.publication.gradle.kts
  2. ./gradlew clean build publishAllPublicationsToCentralRepository

Local maven

  1. Set X.X-SNAPSHOT version in root.publication.gradle.kts
  2. ./gradlew clean build publishToMavenLocal

License

Copyright 2024 Sergej Shafarenka, www.halfbit.de

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Minimalistic, fast and configurable Logger for Kotlin Multiplatform.

License:Apache License 2.0


Languages

Language:Kotlin 96.4%Language:Swift 2.9%Language:HTML 0.5%Language:CSS 0.2%