Skip to content

Commit

Permalink
issue #21 : js + common will be publisher in version 1.5.5
Browse files Browse the repository at this point in the history
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
oshai committed Jul 28, 2018
1 parent c6e0be6 commit cca07ed
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 38 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ buildscript {

allprojects {
group 'io.github.microutils'
version '1.5.3'
version '1.5.5'

repositories {
mavenCentral()
Expand Down
1 change: 1 addition & 0 deletions kotlin-logging-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ repositories {

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
}
57 changes: 57 additions & 0 deletions kotlin-logging-common/src/main/kotlin/mu/KLogger.kt
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?)
}
13 changes: 13 additions & 0 deletions kotlin-logging-common/src/main/kotlin/mu/KotlinLogging.kt
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
}
57 changes: 57 additions & 0 deletions kotlin-logging-js/src/main/kotlin/mu/KLogger.kt
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?)
}
14 changes: 14 additions & 0 deletions kotlin-logging-js/src/main/kotlin/mu/KotlinLogging.kt
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 kotlin-logging-js/src/main/kotlin/mu/KotlinLoggingLevel.kt
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 kotlin-logging-js/src/main/kotlin/mu/internal/KLoggerJS.kt
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
}
}
45 changes: 13 additions & 32 deletions kotlin-logging-jvm/src/main/kotlin/mu/KLogger.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package mu

import mu.internal.toStringSafe
import org.slf4j.Logger

/**
* An extension for [Logger] with Lazy message evaluation
* example:
* logger.info{"this is $lazy evaluated string"}
*/
interface KLogger : Logger {
actual interface KLogger : Logger {

/**
* The actual logger executing logging
Expand All @@ -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?)


}
6 changes: 3 additions & 3 deletions kotlin-logging-jvm/src/main/kotlin/mu/KotlinLogging.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading

0 comments on commit cca07ed

Please sign in to comment.