KFlogger: Experimental KMP port of Flogger
NOTE: This is an experimental project and is not meant to be used in production environments.
Target Platform | Target preset |
---|---|
Kotlin/JVM |
|
iOS |
|
// build.gradle.kts
implementation("com.giancarlobuenaflor:kflogger:<version>")
import com.giancarlobuenaflor.kflogger.KFluentLogger
class LoggingClass {
val logger = KFluentLogger.forEnclosingClass()
fun loggingMethod() {
logger.atWarning().log("my message: %s", "Hello World")
}
}
- logging simple messages without arguments at all log levels ->
log.atWarning().log("my message")
orlog.atFine().log("my message")
- logging messages with printf format ->
log.atWarning("my message: $s", "test")
although this is only limited to `%s, %d, %i, %f% due to lack of implementation on the iOS side. - avoid work at log site with lazy ->
log.atWarning("complex work: $s", KLazyArgs.lazy { "result" })
We use actual typealias
very extensively to make sure we can keep the original Flogger Java code.
This has some weird caveats, for example Java protected does not equal Kotlin protected or Kotlin companion functions cannot be typealiased to static functions on Java.
The workaround here is to ignore those errors and make sure compilation works through tests.
The core functionality will work as usual on JVM.
Most of the code has been directly transferred from Flogger. Changes to the Flogger API to accomodate KMP are very minimal and are the following:
Currently only the api module is available in KMP but adding the other ones should not be an issue since the api
module is already incorporated into KMP.
The iOS implementation has a lot of gaps and many TODOs.
It only contains the core implementations necessary to make the common functionality work.
- OSLog is used as default logging backend.
- The printf format is done manually inside
SimpleLoggerBackend
. This is less than ideal and should be properly implemented viaPrintfMessageParser
and theBaseMessageFormatter
and other relevant classes. findLoggingClass
has been implemented withNSThread.callStackSymbols
. This implementation assumes that the logging class is within the common Kotlin code. So it won't work for native Obj-c or Swift classes.
JVM Flogger contains the original test cases.
The common code is tested via Kruth. The approach here is to take the JVM Flogger tests 1:1 and apply them to Kruth to make sure functionality is consistent across all platforms.
In addition to ensure the actual typealias
work for all defined common code we test compilation of all KMP classes to make sure we don't run into any runtime errors.