Skip to content

renfrowtech/kmp-log

Repository files navigation

kmp-log

Maven Central Android API Android iOS Linux Windows MacOS JVM License

Configurable Logging framework built for Kotlin Multiplatform

Documentation

Full documentation of the Logging framework is available in the documentation

Getting Started

Multiplatform

Gradle

Add the Maven Central repository if it's not already there:

repositories {
    mavenCentral()
}

Kotlin DSL (see badges for platform support)

commonMain {
    dependencies {
        implementation("com.renfrowtech:kmp-log:<latest-version>")
    }
}

Android

Gradle

Add the Maven Central repository if it's not already there:

repositories {
    mavenCentral()
}

Groovy

implementation "com.renfrowtech:kmp-log-android:<latest-version>"

Kotlin DSL

implementation("com.renfrowtech:kmp-log:<latest-version>")

Implementation

Add Log Strategies

In a global scope, specify the implementations of the LoggingStrategy interface to be used for logging.

// The built in implementation of the `LoggingStrategy` interface for logging to Logcat 
Logger.addStrategies(LogcatStrategy())

// The same implementation, specifying the minimum logging level
Logger.addStrategies(LogcatStrategy(minLogLevel = LogLevel.DEBUG))

// A custom LoggingStrategy implementation
Logger.addStrategies(CustomLogStrategy())

Get a logger instance

In any class, use the LoggerDelegate to get a logger instance

class Foo {
    val logger by LoggerDelegate(/*Optionally provide TAG string here, otherwise it will be derived from the class name*/)
    
    //...
}

Log a message

class MainActivity { // Can be any class
    fun foo() {
        //...
        logger.info("Hello world!")
        //...
    }
}

Add data to a log

To add data to the log statement use the withData function and provide a series (vararg) of Pair< String, Any?> or a Map<String, Any?>

class MainActivity { // Can be any class
    fun foo(something: Int) {
        //...
        logger
            .withData("something" to something)
            .info("In foo!")
        //...
    }
}

Add exception to a log

To add exception to the log statement use the withException function

class Foo {
    fun bar(someParam: String) {
        //...
        try {
            // Do something that can throw an exception
        } catch (e: Exception) {
            logger
                .withData("something" to e.message)
                .withException(e)
                .error("Something went wrong in foo") // Any level log is ok here
        }
        //...
    }
}

Note that you can mix withData and withException

If you want the same data included in multiple logs, capture the logger returned from withData or withException

val loggerWithSomething = logger.withData("something" to something)

loggerWithSomething.trace("Detailed information")

About

Configurable Logging framework built for Kotlin Multiplatform

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages