From 90b3c6e71c6be988ca6b89f6a79ab5c16701c816 Mon Sep 17 00:00:00 2001 From: magic-cucumber Date: Wed, 14 Jan 2026 21:09:56 +0800 Subject: [PATCH] add log --- .../kdroidfilter/webview/util/KLogger.kt | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/webview-compose/src/commonMain/kotlin/io/github/kdroidfilter/webview/util/KLogger.kt b/webview-compose/src/commonMain/kotlin/io/github/kdroidfilter/webview/util/KLogger.kt index 51e3a53..8bc4155 100644 --- a/webview-compose/src/commonMain/kotlin/io/github/kdroidfilter/webview/util/KLogger.kt +++ b/webview-compose/src/commonMain/kotlin/io/github/kdroidfilter/webview/util/KLogger.kt @@ -6,26 +6,92 @@ package io.github.kdroidfilter.webview.util * Keep logging simple and dependency-free across platforms. */ interface KLogger { + /** + * Sets the minimum severity level for this logger. + * Only logs with severity greater than or equal to this level will be output. + * + * @param severity The minimum log severity level + */ fun setMinSeverity(severity: KLogSeverity) + /** + * Convenience method to output a Debug level log. + * + * @param tag Optional log tag + * @param msg Lambda expression that returns the log message + */ fun d(tag: String? = null, msg: () -> String) = log(KLogSeverity.Debug, tag, null, msg) + /** + * Convenience method to output an Info level log. + * + * @param tag Optional log tag + * @param msg Lambda expression that returns the log message + */ fun i(tag: String? = null, msg: () -> String) = log(KLogSeverity.Info, tag, null, msg) + /** + * Convenience method to output a Warn level log. + * + * @param tag Optional log tag + * @param msg Lambda expression that returns the log message + */ fun w(tag: String? = null, msg: () -> String) = log(KLogSeverity.Warn, tag, null, msg) + /** + * Convenience method to output an Error level log. + * + * @param t Optional throwable object + * @param tag Optional log tag + * @param msg Lambda expression that returns the log message + */ fun e(t: Throwable? = null, tag: String? = null, msg: () -> String) = log(KLogSeverity.Error, tag, t, msg) + /** + * Outputs a log. + * + * @param severity The log severity level + * @param tag Optional log tag + * @param t Optional throwable object + * @param msg Lambda expression that returns the log message + */ fun log(severity: KLogSeverity, tag: String?, t: Throwable?, msg: () -> String) + /** + * Global logger object. + * + * As a global logger, all logging throughout the application will ultimately call + * the companion object's logging methods. These methods will forward logs to all + * added loggers. + * + * Note: The companion object's [setMinSeverity] will set the minimum output level + * for all registered loggers. + */ companion object : KLogger { private val loggers = mutableListOf(DefaultKLogger) + /** + * Adds a custom logger to the global logger manager. + * + * Once added, all logs output through the companion object will be forwarded to this logger. + * Multiple custom loggers can be added via this method to achieve multi-target logging + * (e.g., file, remote server, etc.). + * + * @param logger The logger instance to add + */ fun addLogger(logger: KLogger) { loggers.add(logger) } + /** + * Removes the specified logger from the global logger manager. + * + * After removal, this logger will no longer receive any log messages. + * Note: The default [DefaultKLogger] cannot be removed. + * + * @param logger The logger instance to remove + */ fun removeLogger(logger: KLogger) { loggers.remove(logger) }