
Enables platform-specific debug logging with Android logcat and file logging. Offers configurable logging options, including trace, debug, error levels, and supports diagnostic information inclusion.
Kotlin Multiplatform Logs library. Supporting file and platform specific debug logging. Project specification
KMPLogs is available on Maven Central. In your root project build.gradle.kts file (or settings.gradle file) add mavenCentral() to repositories.
repositories {
mavenCentral()
}Then in your modules add kmplogs-api dependency. Latest version: .
sourceSets {
commonMain.dependencies {
implementation("io.github.sd155:kmplogs-api:<version>") //Lightweight Logger API
}
}And in your application layer module add kmplogs-core dependency. Latest version: .
sourceSets {
androidMain.dependencies {
implementation("io.github.sd155:kmplogs-core:<version>") //Logs implementation for Android
}
}Configure logs before use. Note, that no logging is enabled by default. You may reconfigure logs at any time. Note, that you need not to re-instantiate your loggers.
AndroidLoggerConfigurator()
// Enable file logging.
.enableFileLogging(
appContext = context,
external = false, // Use internal storage
maxBackupFiles = 5, // Keep 5 backup files
logFileSizeMb = 10 // Rotate at 10MB
)
// Enable file logging with defaults: internal, 5 backup files, 10Mb log file threshold.
.enableFileLogging(appContext = context)
// Disable file logging.
.disableFileLogging()
// Enable logcat logging.
.enableLogcatLogging()
// Disable logcat logging.
.disableLogcatLogging()
// Enable TRACE level logging (most detailed).
.enableTraceLogging()
// Disable TRACE level logging.
.disableTraceLogging()
// Enable DEBUG level logging.
.enableDebugLogging()
// Disable DEBUG level logging.
.disableDebugLogging()To log something - create logger instances and provide them for your components in your favorite DI pattern.
// Use top level factory method to create an instance of Logger.
val logger = createAndroidLogger(sourceTag = "YourComponentTag")After configuring above steps this is how you can use:
// Just use proper Logger methods in your component.
logger.trace(event = "Starting some usecase.")
// To log some exception.
logger.error(event = "Some usecase failure.", e = e)
// You may put some optional objects for diagnostic reason.
logger.debug(event = "Some debug point.", diagnostics = listOf(httpRequest, httpResponse))Note: To enable any logging, you must explicitly configure it using AndroidLoggerConfigurator.
Each log entry is stored as a JSON object with the following structure:
{
"time": "ISO-8601 timestamp",
"type": "TRACE|DEBUG|INFO|WARNING|ERROR|FATAL",
"source": "component identifier",
"thread": "thread name",
"event": "event/message",
"error": {
"message": "error message",
"stacktrace": "stack trace"
},
"diagnostics": {
"class name": { serialized to JSON instance of class }
}
}Kotlin Multiplatform Logs library. Supporting file and platform specific debug logging. Project specification
KMPLogs is available on Maven Central. In your root project build.gradle.kts file (or settings.gradle file) add mavenCentral() to repositories.
repositories {
mavenCentral()
}Then in your modules add kmplogs-api dependency. Latest version: .
sourceSets {
commonMain.dependencies {
implementation("io.github.sd155:kmplogs-api:<version>") //Lightweight Logger API
}
}And in your application layer module add kmplogs-core dependency. Latest version: .
sourceSets {
androidMain.dependencies {
implementation("io.github.sd155:kmplogs-core:<version>") //Logs implementation for Android
}
}Configure logs before use. Note, that no logging is enabled by default. You may reconfigure logs at any time. Note, that you need not to re-instantiate your loggers.
AndroidLoggerConfigurator()
// Enable file logging.
.enableFileLogging(
appContext = context,
external = false, // Use internal storage
maxBackupFiles = 5, // Keep 5 backup files
logFileSizeMb = 10 // Rotate at 10MB
)
// Enable file logging with defaults: internal, 5 backup files, 10Mb log file threshold.
.enableFileLogging(appContext = context)
// Disable file logging.
.disableFileLogging()
// Enable logcat logging.
.enableLogcatLogging()
// Disable logcat logging.
.disableLogcatLogging()
// Enable TRACE level logging (most detailed).
.enableTraceLogging()
// Disable TRACE level logging.
.disableTraceLogging()
// Enable DEBUG level logging.
.enableDebugLogging()
// Disable DEBUG level logging.
.disableDebugLogging()To log something - create logger instances and provide them for your components in your favorite DI pattern.
// Use top level factory method to create an instance of Logger.
val logger = createAndroidLogger(sourceTag = "YourComponentTag")After configuring above steps this is how you can use:
// Just use proper Logger methods in your component.
logger.trace(event = "Starting some usecase.")
// To log some exception.
logger.error(event = "Some usecase failure.", e = e)
// You may put some optional objects for diagnostic reason.
logger.debug(event = "Some debug point.", diagnostics = listOf(httpRequest, httpResponse))Note: To enable any logging, you must explicitly configure it using AndroidLoggerConfigurator.
Each log entry is stored as a JSON object with the following structure:
{
"time": "ISO-8601 timestamp",
"type": "TRACE|DEBUG|INFO|WARNING|ERROR|FATAL",
"source": "component identifier",
"thread": "thread name",
"event": "event/message",
"error": {
"message": "error message",
"stacktrace": "stack trace"
},
"diagnostics": {
"class name": { serialized to JSON instance of class }
}
}