Configurable Logging framework built for Kotlin Multiplatform
Full documentation of the Logging framework is available in the documentation
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>")
}
}
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>")
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())
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*/)
//...
}
class MainActivity { // Can be any class
fun foo() {
//...
logger.info("Hello world!")
//...
}
}
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!")
//...
}
}
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")