From cca07ed692f66c2ac09056a7ba817bf36eb1f820 Mon Sep 17 00:00:00 2001 From: oshai Date: Sun, 29 Jul 2018 01:02:24 +0300 Subject: [PATCH] issue #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 --- build.gradle | 2 +- kotlin-logging-common/build.gradle | 1 + .../src/main/kotlin/mu/KLogger.kt | 57 ++++++++++++ .../src/main/kotlin/mu/KotlinLogging.kt | 13 +++ .../src/main/kotlin/mu/KLogger.kt | 57 ++++++++++++ .../src/main/kotlin/mu/KotlinLogging.kt | 14 +++ .../src/main/kotlin/mu/KotlinLoggingLevel.kt | 13 +++ .../src/main/kotlin/mu/internal/KLoggerJS.kt | 89 +++++++++++++++++++ .../src/main/kotlin/mu/KLogger.kt | 45 +++------- .../src/main/kotlin/mu/KotlinLogging.kt | 6 +- .../mu/internal/LocationIgnorantKLogger.kt | 73 ++++++++++++++- versions.gradle | 2 +- 12 files changed, 334 insertions(+), 38 deletions(-) create mode 100644 kotlin-logging-common/src/main/kotlin/mu/KLogger.kt create mode 100644 kotlin-logging-common/src/main/kotlin/mu/KotlinLogging.kt create mode 100644 kotlin-logging-js/src/main/kotlin/mu/KLogger.kt create mode 100644 kotlin-logging-js/src/main/kotlin/mu/KotlinLogging.kt create mode 100644 kotlin-logging-js/src/main/kotlin/mu/KotlinLoggingLevel.kt create mode 100644 kotlin-logging-js/src/main/kotlin/mu/internal/KLoggerJS.kt diff --git a/build.gradle b/build.gradle index 59565de9..e3780b73 100644 --- a/build.gradle +++ b/build.gradle @@ -14,7 +14,7 @@ buildscript { allprojects { group 'io.github.microutils' - version '1.5.3' + version '1.5.5' repositories { mavenCentral() diff --git a/kotlin-logging-common/build.gradle b/kotlin-logging-common/build.gradle index 17286046..e2e927f5 100644 --- a/kotlin-logging-common/build.gradle +++ b/kotlin-logging-common/build.gradle @@ -6,4 +6,5 @@ repositories { dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version" + testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version" } diff --git a/kotlin-logging-common/src/main/kotlin/mu/KLogger.kt b/kotlin-logging-common/src/main/kotlin/mu/KLogger.kt new file mode 100644 index 00000000..4b0200a3 --- /dev/null +++ b/kotlin-logging-common/src/main/kotlin/mu/KLogger.kt @@ -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?) +} diff --git a/kotlin-logging-common/src/main/kotlin/mu/KotlinLogging.kt b/kotlin-logging-common/src/main/kotlin/mu/KotlinLogging.kt new file mode 100644 index 00000000..5b649977 --- /dev/null +++ b/kotlin-logging-common/src/main/kotlin/mu/KotlinLogging.kt @@ -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 +} diff --git a/kotlin-logging-js/src/main/kotlin/mu/KLogger.kt b/kotlin-logging-js/src/main/kotlin/mu/KLogger.kt new file mode 100644 index 00000000..816b38a9 --- /dev/null +++ b/kotlin-logging-js/src/main/kotlin/mu/KLogger.kt @@ -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?) +} diff --git a/kotlin-logging-js/src/main/kotlin/mu/KotlinLogging.kt b/kotlin-logging-js/src/main/kotlin/mu/KotlinLogging.kt new file mode 100644 index 00000000..d807bfb5 --- /dev/null +++ b/kotlin-logging-js/src/main/kotlin/mu/KotlinLogging.kt @@ -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) +} diff --git a/kotlin-logging-js/src/main/kotlin/mu/KotlinLoggingLevel.kt b/kotlin-logging-js/src/main/kotlin/mu/KotlinLoggingLevel.kt new file mode 100644 index 00000000..80c8775e --- /dev/null +++ b/kotlin-logging-js/src/main/kotlin/mu/KotlinLoggingLevel.kt @@ -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 \ No newline at end of file diff --git a/kotlin-logging-js/src/main/kotlin/mu/internal/KLoggerJS.kt b/kotlin-logging-js/src/main/kotlin/mu/internal/KLoggerJS.kt new file mode 100644 index 00000000..3ba6a642 --- /dev/null +++ b/kotlin-logging-js/src/main/kotlin/mu/internal/KLoggerJS.kt @@ -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 + } +} diff --git a/kotlin-logging-jvm/src/main/kotlin/mu/KLogger.kt b/kotlin-logging-jvm/src/main/kotlin/mu/KLogger.kt index 08bb2968..648c33a1 100644 --- a/kotlin-logging-jvm/src/main/kotlin/mu/KLogger.kt +++ b/kotlin-logging-jvm/src/main/kotlin/mu/KLogger.kt @@ -1,6 +1,5 @@ package mu -import mu.internal.toStringSafe import org.slf4j.Logger /** @@ -8,7 +7,7 @@ import org.slf4j.Logger * example: * logger.info{"this is $lazy evaluated string"} */ -interface KLogger : Logger { +actual interface KLogger : Logger { /** * The actual logger executing logging @@ -18,70 +17,52 @@ interface KLogger : Logger { /** * Lazy add a log message if isTraceEnabled is true */ - fun trace(msg: () -> Any?) { - if (isTraceEnabled) trace(msg.toStringSafe()) - } + actual fun trace(msg: () -> Any?) /** * Lazy add a log message if isDebugEnabled is true */ - fun debug(msg: () -> Any?) { - if (isDebugEnabled) debug(msg.toStringSafe()) - } + actual fun debug(msg: () -> Any?) /** * Lazy add a log message if isInfoEnabled is true */ - fun info(msg: () -> Any?) { - if (isInfoEnabled) info(msg.toStringSafe()) - } + actual fun info(msg: () -> Any?) /** * Lazy add a log message if isWarnEnabled is true */ - fun warn(msg: () -> Any?) { - if (isWarnEnabled) warn(msg.toStringSafe()) - } + actual fun warn(msg: () -> Any?) /** * Lazy add a log message if isErrorEnabled is true */ - fun error(msg: () -> Any?) { - if (isErrorEnabled) error(msg.toStringSafe()) - } + actual fun error(msg: () -> Any?) /** * Lazy add a log message with throwable payload if isTraceEnabled is true */ - fun trace(t: Throwable?, msg: () -> Any?) { - if (isTraceEnabled) trace(msg.toStringSafe(), t) - } + actual fun trace(t: Throwable?, msg: () -> Any?) /** * Lazy add a log message with throwable payload if isDebugEnabled is true */ - fun debug(t: Throwable?, msg: () -> Any?) { - if (isDebugEnabled) debug(msg.toStringSafe(), t) - } + actual fun debug(t: Throwable?, msg: () -> Any?) /** * Lazy add a log message with throwable payload if isInfoEnabled is true */ - fun info(t: Throwable?, msg: () -> Any?) { - if (isInfoEnabled) info(msg.toStringSafe(), t) - } + actual fun info(t: Throwable?, msg: () -> Any?) /** * Lazy add a log message with throwable payload if isWarnEnabled is true */ - fun warn(t: Throwable?, msg: () -> Any?) { - if (isWarnEnabled) warn(msg.toStringSafe(), t) - } + actual fun warn(t: Throwable?, msg: () -> Any?) /** * Lazy add a log message with throwable payload if isErrorEnabled is true */ - fun error(t: Throwable?, msg: () -> Any?) { - if (isErrorEnabled) error(msg.toStringSafe(), t) - } + actual fun error(t: Throwable?, msg: () -> Any?) + + } diff --git a/kotlin-logging-jvm/src/main/kotlin/mu/KotlinLogging.kt b/kotlin-logging-jvm/src/main/kotlin/mu/KotlinLogging.kt index e3344393..274a2de1 100644 --- a/kotlin-logging-jvm/src/main/kotlin/mu/KotlinLogging.kt +++ b/kotlin-logging-jvm/src/main/kotlin/mu/KotlinLogging.kt @@ -3,12 +3,12 @@ package mu import mu.internal.KLoggerFactory -object KotlinLogging { +actual object KotlinLogging { /** * This method allow defining the logger in a file in the following way: * val logger = KotlinLogging.logger {} */ - fun logger(func: () -> Unit): KLogger = KLoggerFactory.logger(func) + actual fun logger(func: () -> Unit): KLogger = KLoggerFactory.logger(func) - fun logger(name: String): KLogger = KLoggerFactory.logger(name) + actual fun logger(name: String): KLogger = KLoggerFactory.logger(name) } diff --git a/kotlin-logging-jvm/src/main/kotlin/mu/internal/LocationIgnorantKLogger.kt b/kotlin-logging-jvm/src/main/kotlin/mu/internal/LocationIgnorantKLogger.kt index 1a59e02c..3e2dd710 100644 --- a/kotlin-logging-jvm/src/main/kotlin/mu/internal/LocationIgnorantKLogger.kt +++ b/kotlin-logging-jvm/src/main/kotlin/mu/internal/LocationIgnorantKLogger.kt @@ -10,4 +10,75 @@ import org.slf4j.Logger * Hence no implemented methods */ internal class LocationIgnorantKLogger(override val underlyingLogger: Logger) - : KLogger, Logger by underlyingLogger + : KLogger, Logger by underlyingLogger { + + /** + * Lazy add a log message if isTraceEnabled is true + */ + override fun trace(msg: () -> Any?) { + if (isTraceEnabled) trace(msg.toStringSafe()) + } + + /** + * Lazy add a log message if isDebugEnabled is true + */ + override fun debug(msg: () -> Any?) { + if (isDebugEnabled) debug(msg.toStringSafe()) + } + + /** + * Lazy add a log message if isInfoEnabled is true + */ + override fun info(msg: () -> Any?) { + if (isInfoEnabled) info(msg.toStringSafe()) + } + + /** + * Lazy add a log message if isWarnEnabled is true + */ + override fun warn(msg: () -> Any?) { + if (isWarnEnabled) warn(msg.toStringSafe()) + } + + /** + * Lazy add a log message if isErrorEnabled is true + */ + override fun error(msg: () -> Any?) { + if (isErrorEnabled) error(msg.toStringSafe()) + } + + /** + * Lazy add a log message with throwable payload if isTraceEnabled is true + */ + override fun trace(t: Throwable?, msg: () -> Any?) { + if (isTraceEnabled) trace(msg.toStringSafe(), t) + } + + /** + * Lazy add a log message with throwable payload if isDebugEnabled is true + */ + override fun debug(t: Throwable?, msg: () -> Any?) { + if (isDebugEnabled) debug(msg.toStringSafe(), t) + } + + /** + * Lazy add a log message with throwable payload if isInfoEnabled is true + */ + override fun info(t: Throwable?, msg: () -> Any?) { + if (isInfoEnabled) info(msg.toStringSafe(), t) + } + + /** + * Lazy add a log message with throwable payload if isWarnEnabled is true + */ + override fun warn(t: Throwable?, msg: () -> Any?) { + if (isWarnEnabled) warn(msg.toStringSafe(), t) + } + + /** + * Lazy add a log message with throwable payload if isErrorEnabled is true + */ + override fun error(t: Throwable?, msg: () -> Any?) { + if (isErrorEnabled) error(msg.toStringSafe(), t) + } +} diff --git a/versions.gradle b/versions.gradle index 328a91c3..9a803e61 100644 --- a/versions.gradle +++ b/versions.gradle @@ -1,2 +1,2 @@ -ext.kotlin_version='1.2.20' +ext.kotlin_version='1.2.51' ext.sl4j_version='1.7.25'