rwachters

KMP Logger

Oct 27th, 2025 (edited)
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 7.08 KB | Software | 0 0
  1. // file: app/src/commonMain/kotlin/eu/torvian/chatbot/app/utils/misc/KmpLogger.kt
  2. package eu.torvian.chatbot.app.utils.misc
  3.  
  4. /**
  5.  * Common logging interface for Kotlin Multiplatform.
  6.  * This interface defines the logging API available in common code.
  7.  * Each platform will provide its actual implementation.
  8.  */
  9. interface KmpLogger {
  10.     fun trace(message: String, throwable: Throwable? = null)
  11.     fun debug(message: String, throwable: Throwable? = null)
  12.     fun info(message: String, throwable: Throwable? = null)
  13.     fun warn(message: String, throwable: Throwable? = null)
  14.     fun error(message: String, throwable: Throwable? = null)
  15.     fun fatal(message: String, throwable: Throwable? = null)
  16. }
  17.  
  18. /**
  19.  * Expected top-level function that acts as a factory for [KmpLogger] instances.
  20.  * Each platform will provide an 'actual' implementation of this function.
  21.  *
  22.  * This is the most stable and recommended approach for platform-specific utilities
  23.  * when avoiding DI framework boilerplate in every consuming class and ensuring
  24.  * compatibility with parallel test execution.
  25.  */
  26. expect fun createKmpLogger(tag: String): KmpLogger
  27.  
  28. /**
  29.  * Helper function to easily get a [KmpLogger] instance for a class.
  30.  * This function uses the platform-specific `createKmpLogger` factory function.
  31.  *
  32.  * Usage: `private val logger = kmpLogger<MyClass>()`
  33.  */
  34. inline fun <reified T> kmpLogger(): KmpLogger {
  35.     return createKmpLogger(T::class.simpleName ?: "Unknown")
  36. }
  37.  
  38. // file: app/src/androidMain/kotlin/eu/torvian/chatbot/app/utils/misc/KmpLogger.android.kt
  39. package eu.torvian.chatbot.app.utils.misc
  40.  
  41. import android.util.Log
  42.  
  43. /**
  44.  * Actual implementation of [KmpLogger] for the Android target, using Android's Log API.
  45.  */
  46. class AndroidKmpLogger(private val tag: String) : KmpLogger {
  47.     override fun trace(message: String, throwable: Throwable?) {
  48.         Log.v(tag, message, throwable)
  49.     }
  50.  
  51.     override fun debug(message: String, throwable: Throwable?) {
  52.         Log.d(tag, message, throwable)
  53.     }
  54.  
  55.     override fun info(message: String, throwable: Throwable?) {
  56.         Log.i(tag, message, throwable)
  57.     }
  58.  
  59.     override fun warn(message: String, throwable: Throwable?) {
  60.         Log.w(tag, message, throwable)
  61.     }
  62.  
  63.     override fun error(message: String, throwable: Throwable?) {
  64.         Log.e(tag, message, throwable)
  65.     }
  66.  
  67.     override fun fatal(message: String, throwable: Throwable?) {
  68.         Log.wtf(tag, message, throwable)
  69.     }
  70. }
  71.  
  72. /**
  73.  * Actual implementation of the [createKmpLogger] factory function for the Android target.
  74.  * This function provides [AndroidKmpLogger] instances.
  75.  */
  76. actual fun createKmpLogger(tag: String): KmpLogger {
  77.     return AndroidKmpLogger(tag)
  78. }
  79.  
  80.  
  81.  
  82. // file: app/src/desktopMain/kotlin/eu/torvian/chatbot/app/utils/misc/createKmpLogger.desktop.kt
  83. package eu.torvian.chatbot.app.utils.misc
  84.  
  85. import org.apache.logging.log4j.LogManager
  86.  
  87. /**
  88.  * Actual implementation of [KmpLogger] for the JVM (Desktop) target, using Log4j2.
  89.  */
  90. class DesktopKmpLogger(private val tag: String) : KmpLogger {
  91.     private val logger = LogManager.getLogger(tag)
  92.  
  93.     override fun trace(message: String, throwable: Throwable?) {
  94.         logger.trace(message, throwable)
  95.     }
  96.  
  97.     override fun debug(message: String, throwable: Throwable?) {
  98.         logger.debug(message, throwable)
  99.     }
  100.  
  101.     override fun info(message: String, throwable: Throwable?) {
  102.         logger.info(message, throwable)
  103.     }
  104.  
  105.     override fun warn(message: String, throwable: Throwable?) {
  106.         logger.warn(message, throwable)
  107.     }
  108.  
  109.     override fun error(message: String, throwable: Throwable?) {
  110.         logger.error(message, throwable)
  111.     }
  112.  
  113.     override fun fatal(message: String, throwable: Throwable?) {
  114.         logger.fatal(message, throwable)
  115.     }
  116. }
  117.  
  118. /**
  119.  * Actual implementation of the [createKmpLogger] factory function for the JVM (Desktop) target.
  120.  * This function provides [DesktopKmpLogger] instances.
  121.  */
  122. actual fun createKmpLogger(tag: String): KmpLogger {
  123.     return DesktopKmpLogger(tag)
  124. }
  125.  
  126. // file: app/src/wasmJsMain/kotlin/eu/torvian/chatbot/app/utils/misc/createKmpLogger.wasmJs.kt
  127. package eu.torvian.chatbot.app.utils.misc
  128.  
  129. /**
  130.  * A simplified external interface for the browser's Console API, compatible with Kotlin/Wasm.
  131.  * It explicitly uses String for arguments, as per Wasm JS interop rules.
  132.  *
  133.  * Console methods typically take a primary message and then optional additional arguments.
  134.  * We model this by having a `message: String` and `vararg optionalParams: String`.
  135.  */
  136. external interface Console {
  137.     // Note: The actual JS console functions take Any, but for Wasm interop,
  138.     // we limit it to String as that's what we primarily pass.
  139.     fun debug(message: String, vararg optionalParams: String)
  140.     fun info(message: String, vararg optionalParams: String)
  141.     fun warn(message: String, vararg optionalParams: String)
  142.     fun error(message: String, vararg optionalParams: String)
  143. }
  144.  
  145. /**
  146.  * The global browser `console` object.
  147.  * Declared 'external val' to indicate it's a JavaScript global variable.
  148.  */
  149. external val console: Console
  150.  
  151. /**
  152.  * Actual implementation of [KmpLogger] for the Kotlin/Wasm (JavaScript) target,
  153.  * using the browser's `console` API.
  154.  */
  155. class WasmKmpLogger(private val tag: String) : KmpLogger {
  156.     override fun trace(message: String, throwable: Throwable?) {
  157.         if (throwable != null) {
  158.             console.debug("[$tag] $message ${throwable.stackTraceToString()}")
  159.         } else {
  160.             console.debug("[$tag] $message")
  161.         }
  162.     }
  163.  
  164.     override fun debug(message: String, throwable: Throwable?) {
  165.         if (throwable != null) {
  166.             console.debug("[$tag] $message ${throwable.stackTraceToString()}")
  167.         } else {
  168.             console.debug("[$tag] $message")
  169.         }
  170.     }
  171.  
  172.     override fun info(message: String, throwable: Throwable?) {
  173.         if (throwable != null) {
  174.             console.info("[$tag] $message ${throwable.stackTraceToString()}")
  175.         } else {
  176.             console.info("[$tag] $message")
  177.         }
  178.     }
  179.  
  180.     override fun warn(message: String, throwable: Throwable?) {
  181.         if (throwable != null) {
  182.             console.warn("[$tag] $message ${throwable.stackTraceToString()}")
  183.         } else {
  184.             console.warn("[$tag] $message")
  185.         }
  186.     }
  187.  
  188.     override fun error(message: String, throwable: Throwable?) {
  189.         if (throwable != null) {
  190.             console.error("[$tag] $message ${throwable.stackTraceToString()}")
  191.         } else {
  192.             console.error("[$tag] $message")
  193.         }
  194.     }
  195.  
  196.     override fun fatal(message: String, throwable: Throwable?) {
  197.         if (throwable != null) {
  198.             console.error("[$tag] $message ${throwable.stackTraceToString()}")
  199.         } else {
  200.             console.error("[$tag] $message")
  201.         }
  202.     }
  203. }
  204.  
  205. /**
  206.  * Actual implementation of the [createKmpLogger] factory function for the Kotlin/Wasm (JavaScript) target.
  207.  * This function provides [WasmKmpLogger] instances.
  208.  */
  209. actual fun createKmpLogger(tag: String): KmpLogger {
  210.     return WasmKmpLogger(tag)
  211. }
Advertisement
Add Comment
Please, Sign In to add comment