Skip to content

buenaflor/KFlogger

Repository files navigation

KFlogger: Experimental KMP port of Flogger

NOTE: This is an experimental project and is not meant to be used in production environments.

Supported Platforms

Target Platform Target preset
Kotlin/JVM
  • jvm
iOS
  • iosArm64
  • iosX64
  • iosSimulatorArm64

Install

// build.gradle.kts

implementation("com.giancarlobuenaflor:kflogger:<version>")

Usage

import com.giancarlobuenaflor.kflogger.KFluentLogger

class LoggingClass {
  val logger = KFluentLogger.forEnclosingClass()

  fun loggingMethod() {
    logger.atWarning().log("my message: %s", "Hello World")
  }
}

Current state as of commit e724eeba1fd440b20bf8e865a1d735a0dd238ec0

Common functionality

  • logging simple messages without arguments at all log levels -> log.atWarning().log("my message") or log.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" })

Implementation approach

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.

JVM Flogger

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.

iOS

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 via PrintfMessageParser and the BaseMessageFormatter and other relevant classes.
  • findLoggingClass has been implemented with NSThread.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.

Testing

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.