forked from oshai/kotlin-logging
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue oshai#21 : js + common will be publisher in version 1.5.5
add common and js jars (will be in https://github.com/MicroUtils/kotlin-logging/releases) common module have expect for 2 main classes: KotlinLogging, KLogger js implementation includes writing to console, and a class KotlinLoggingLevel that defines the log level in LOG_LEVEL member default level is info for js implementation
- Loading branch information
1 parent
288f04e
commit 94b6c41
Showing
12 changed files
with
334 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package mu | ||
|
||
|
||
|
||
expect interface KLogger { | ||
|
||
|
||
/** | ||
* Lazy add a log message if isTraceEnabled is true | ||
*/ | ||
fun trace(msg: () -> Any?) | ||
|
||
/** | ||
* Lazy add a log message if isDebugEnabled is true | ||
*/ | ||
fun debug(msg: () -> Any?) | ||
|
||
/** | ||
* Lazy add a log message if isInfoEnabled is true | ||
*/ | ||
fun info(msg: () -> Any?) | ||
|
||
/** | ||
* Lazy add a log message if isWarnEnabled is true | ||
*/ | ||
fun warn(msg: () -> Any?) | ||
|
||
/** | ||
* Lazy add a log message if isErrorEnabled is true | ||
*/ | ||
fun error(msg: () -> Any?) | ||
|
||
/** | ||
* Lazy add a log message with throwable payload if isTraceEnabled is true | ||
*/ | ||
fun trace(t: Throwable?, msg: () -> Any?) | ||
|
||
/** | ||
* Lazy add a log message with throwable payload if isDebugEnabled is true | ||
*/ | ||
fun debug(t: Throwable?, msg: () -> Any?) | ||
|
||
/** | ||
* Lazy add a log message with throwable payload if isInfoEnabled is true | ||
*/ | ||
fun info(t: Throwable?, msg: () -> Any?) | ||
|
||
/** | ||
* Lazy add a log message with throwable payload if isWarnEnabled is true | ||
*/ | ||
fun warn(t: Throwable?, msg: () -> Any?) | ||
|
||
/** | ||
* Lazy add a log message with throwable payload if isErrorEnabled is true | ||
*/ | ||
fun error(t: Throwable?, msg: () -> Any?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package mu | ||
|
||
|
||
|
||
expect object KotlinLogging { | ||
/** | ||
* This method allow defining the logger in a file in the following way: | ||
* val logger = KotlinLogging.logger {} | ||
*/ | ||
fun logger(func: () -> Unit): KLogger | ||
|
||
fun logger(name: String): KLogger | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package mu | ||
|
||
|
||
|
||
actual interface KLogger { | ||
|
||
|
||
/** | ||
* Lazy add a log message if isTraceEnabled is true | ||
*/ | ||
actual fun trace(msg: () -> Any?) | ||
|
||
/** | ||
* Lazy add a log message if isDebugEnabled is true | ||
*/ | ||
actual fun debug(msg: () -> Any?) | ||
|
||
/** | ||
* Lazy add a log message if isInfoEnabled is true | ||
*/ | ||
actual fun info(msg: () -> Any?) | ||
|
||
/** | ||
* Lazy add a log message if isWarnEnabled is true | ||
*/ | ||
actual fun warn(msg: () -> Any?) | ||
|
||
/** | ||
* Lazy add a log message if isErrorEnabled is true | ||
*/ | ||
actual fun error(msg: () -> Any?) | ||
|
||
/** | ||
* Lazy add a log message with throwable payload if isTraceEnabled is true | ||
*/ | ||
actual fun trace(t: Throwable?, msg: () -> Any?) | ||
|
||
/** | ||
* Lazy add a log message with throwable payload if isDebugEnabled is true | ||
*/ | ||
actual fun debug(t: Throwable?, msg: () -> Any?) | ||
|
||
/** | ||
* Lazy add a log message with throwable payload if isInfoEnabled is true | ||
*/ | ||
actual fun info(t: Throwable?, msg: () -> Any?) | ||
|
||
/** | ||
* Lazy add a log message with throwable payload if isWarnEnabled is true | ||
*/ | ||
actual fun warn(t: Throwable?, msg: () -> Any?) | ||
|
||
/** | ||
* Lazy add a log message with throwable payload if isErrorEnabled is true | ||
*/ | ||
actual fun error(t: Throwable?, msg: () -> Any?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package mu | ||
|
||
import mu.internal.KLoggerJS | ||
|
||
|
||
actual object KotlinLogging { | ||
/** | ||
* This method allow defining the logger in a file in the following way: | ||
* val logger = KotlinLogging.logger {} | ||
*/ | ||
actual fun logger(func: () -> Unit): KLogger = KLoggerJS(func::class.js.name) | ||
|
||
actual fun logger(name: String): KLogger = KLoggerJS(name) | ||
} |
13 changes: 13 additions & 0 deletions
13
kotlin-logging-js/src/main/kotlin/mu/KotlinLoggingLevel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package mu | ||
|
||
var LOG_LEVEL = KotlinLoggingLevel.INFO | ||
|
||
enum class KotlinLoggingLevel { | ||
TRACE, | ||
DEBUG, | ||
INFO, | ||
WARN, | ||
ERROR | ||
} | ||
|
||
fun KotlinLoggingLevel.isLoggingEnabled() = this.ordinal >= LOG_LEVEL.ordinal |
89 changes: 89 additions & 0 deletions
89
kotlin-logging-js/src/main/kotlin/mu/internal/KLoggerJS.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package mu.internal | ||
|
||
import mu.KLogger | ||
import mu.KotlinLoggingLevel | ||
import mu.isLoggingEnabled | ||
|
||
class KLoggerJS(private val loggerName: String) : KLogger { | ||
|
||
override fun trace(msg: () -> Any?) { | ||
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) { | ||
console.log("TRACE: [$loggerName] ${msg.toStringSafe()}") | ||
} | ||
} | ||
|
||
override fun debug(msg: () -> Any?) { | ||
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) { | ||
console.log("DEBUG: [$loggerName] ${msg.toStringSafe()}") | ||
} | ||
} | ||
|
||
override fun info(msg: () -> Any?) { | ||
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) { | ||
console.info("INFO: [$loggerName] ${msg.toStringSafe()}") | ||
} | ||
} | ||
|
||
override fun warn(msg: () -> Any?) { | ||
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) { | ||
console.warn("WARN: [$loggerName] ${msg.toStringSafe()}") | ||
} | ||
} | ||
|
||
override fun error(msg: () -> Any?) { | ||
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) { | ||
console.error("ERROR: [$loggerName] ${msg.toStringSafe()}") | ||
} | ||
} | ||
|
||
override fun trace(t: Throwable?, msg: () -> Any?) { | ||
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) { | ||
console.log("TRACE: [$loggerName] ${msg.toStringSafe()}${t.throwableToString()}") | ||
} | ||
} | ||
|
||
override fun debug(t: Throwable?, msg: () -> Any?) { | ||
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) { | ||
console.log("DEBUG: [$loggerName] ${msg.toStringSafe()}${t.throwableToString()}") | ||
} | ||
} | ||
|
||
override fun info(t: Throwable?, msg: () -> Any?) { | ||
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) { | ||
console.info("INFO: [$loggerName] ${msg.toStringSafe()}${t.throwableToString()}") | ||
} | ||
} | ||
|
||
override fun warn(t: Throwable?, msg: () -> Any?) { | ||
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) { | ||
console.warn("WARN: [$loggerName] ${msg.toStringSafe()}${t.throwableToString()}") | ||
} | ||
} | ||
|
||
override fun error(t: Throwable?, msg: () -> Any?) { | ||
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) { | ||
console.error("ERROR: [$loggerName] ${msg.toStringSafe()}${t.throwableToString()}") | ||
} | ||
} | ||
|
||
private fun (() -> Any?).toStringSafe(): String { | ||
try { | ||
return invoke().toString() | ||
} catch (e: Exception) { | ||
return "Log message invocation failed: $e" | ||
} | ||
} | ||
|
||
private fun Throwable?.throwableToString(): String { | ||
if (this == null) { | ||
return "" | ||
} | ||
var msg = "" | ||
var current = this | ||
while (current != null && current.cause != current) { | ||
msg += ", Caused by: '${current.message}'" | ||
current = current.cause | ||
} | ||
return msg | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.