Skip to content

Commit 1203fe8

Browse files
authored
Merge pull request #15 from magic-cucumber/make-logger-replacable
and docs
2 parents 4b2341e + 90b3c6e commit 1203fe8

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

  • webview-compose/src/commonMain/kotlin/io/github/kdroidfilter/webview/util

webview-compose/src/commonMain/kotlin/io/github/kdroidfilter/webview/util/KLogger.kt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,92 @@ package io.github.kdroidfilter.webview.util
66
* Keep logging simple and dependency-free across platforms.
77
*/
88
interface KLogger {
9+
/**
10+
* Sets the minimum severity level for this logger.
11+
* Only logs with severity greater than or equal to this level will be output.
12+
*
13+
* @param severity The minimum log severity level
14+
*/
915
fun setMinSeverity(severity: KLogSeverity)
1016

17+
/**
18+
* Convenience method to output a Debug level log.
19+
*
20+
* @param tag Optional log tag
21+
* @param msg Lambda expression that returns the log message
22+
*/
1123
fun d(tag: String? = null, msg: () -> String) = log(KLogSeverity.Debug, tag, null, msg)
1224

25+
/**
26+
* Convenience method to output an Info level log.
27+
*
28+
* @param tag Optional log tag
29+
* @param msg Lambda expression that returns the log message
30+
*/
1331
fun i(tag: String? = null, msg: () -> String) = log(KLogSeverity.Info, tag, null, msg)
1432

33+
/**
34+
* Convenience method to output a Warn level log.
35+
*
36+
* @param tag Optional log tag
37+
* @param msg Lambda expression that returns the log message
38+
*/
1539
fun w(tag: String? = null, msg: () -> String) = log(KLogSeverity.Warn, tag, null, msg)
1640

41+
/**
42+
* Convenience method to output an Error level log.
43+
*
44+
* @param t Optional throwable object
45+
* @param tag Optional log tag
46+
* @param msg Lambda expression that returns the log message
47+
*/
1748
fun e(t: Throwable? = null, tag: String? = null, msg: () -> String) = log(KLogSeverity.Error, tag, t, msg)
1849

50+
/**
51+
* Outputs a log.
52+
*
53+
* @param severity The log severity level
54+
* @param tag Optional log tag
55+
* @param t Optional throwable object
56+
* @param msg Lambda expression that returns the log message
57+
*/
1958
fun log(severity: KLogSeverity, tag: String?, t: Throwable?, msg: () -> String)
2059

2160

61+
/**
62+
* Global logger object.
63+
*
64+
* As a global logger, all logging throughout the application will ultimately call
65+
* the companion object's logging methods. These methods will forward logs to all
66+
* added loggers.
67+
*
68+
* Note: The companion object's [setMinSeverity] will set the minimum output level
69+
* for all registered loggers.
70+
*/
2271
companion object : KLogger {
2372
private val loggers = mutableListOf<KLogger>(DefaultKLogger)
2473

74+
/**
75+
* Adds a custom logger to the global logger manager.
76+
*
77+
* Once added, all logs output through the companion object will be forwarded to this logger.
78+
* Multiple custom loggers can be added via this method to achieve multi-target logging
79+
* (e.g., file, remote server, etc.).
80+
*
81+
* @param logger The logger instance to add
82+
*/
2583
fun addLogger(logger: KLogger) {
2684
loggers.add(logger)
2785
}
2886

87+
/**
88+
* Removes the specified logger from the global logger manager.
89+
*
90+
* After removal, this logger will no longer receive any log messages.
91+
* Note: The default [DefaultKLogger] cannot be removed.
92+
*
93+
* @param logger The logger instance to remove
94+
*/
2995
fun removeLogger(logger: KLogger) {
3096
loggers.remove(logger)
3197
}

0 commit comments

Comments
 (0)