Skip to content

Commit cca07ed

Browse files
committed
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
1 parent c6e0be6 commit cca07ed

File tree

12 files changed

+334
-38
lines changed

12 files changed

+334
-38
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ buildscript {
1414

1515
allprojects {
1616
group 'io.github.microutils'
17-
version '1.5.3'
17+
version '1.5.5'
1818

1919
repositories {
2020
mavenCentral()

kotlin-logging-common/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ repositories {
66

77
dependencies {
88
compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
9+
testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
910
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package mu
2+
3+
4+
5+
expect interface KLogger {
6+
7+
8+
/**
9+
* Lazy add a log message if isTraceEnabled is true
10+
*/
11+
fun trace(msg: () -> Any?)
12+
13+
/**
14+
* Lazy add a log message if isDebugEnabled is true
15+
*/
16+
fun debug(msg: () -> Any?)
17+
18+
/**
19+
* Lazy add a log message if isInfoEnabled is true
20+
*/
21+
fun info(msg: () -> Any?)
22+
23+
/**
24+
* Lazy add a log message if isWarnEnabled is true
25+
*/
26+
fun warn(msg: () -> Any?)
27+
28+
/**
29+
* Lazy add a log message if isErrorEnabled is true
30+
*/
31+
fun error(msg: () -> Any?)
32+
33+
/**
34+
* Lazy add a log message with throwable payload if isTraceEnabled is true
35+
*/
36+
fun trace(t: Throwable?, msg: () -> Any?)
37+
38+
/**
39+
* Lazy add a log message with throwable payload if isDebugEnabled is true
40+
*/
41+
fun debug(t: Throwable?, msg: () -> Any?)
42+
43+
/**
44+
* Lazy add a log message with throwable payload if isInfoEnabled is true
45+
*/
46+
fun info(t: Throwable?, msg: () -> Any?)
47+
48+
/**
49+
* Lazy add a log message with throwable payload if isWarnEnabled is true
50+
*/
51+
fun warn(t: Throwable?, msg: () -> Any?)
52+
53+
/**
54+
* Lazy add a log message with throwable payload if isErrorEnabled is true
55+
*/
56+
fun error(t: Throwable?, msg: () -> Any?)
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package mu
2+
3+
4+
5+
expect object KotlinLogging {
6+
/**
7+
* This method allow defining the logger in a file in the following way:
8+
* val logger = KotlinLogging.logger {}
9+
*/
10+
fun logger(func: () -> Unit): KLogger
11+
12+
fun logger(name: String): KLogger
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package mu
2+
3+
4+
5+
actual interface KLogger {
6+
7+
8+
/**
9+
* Lazy add a log message if isTraceEnabled is true
10+
*/
11+
actual fun trace(msg: () -> Any?)
12+
13+
/**
14+
* Lazy add a log message if isDebugEnabled is true
15+
*/
16+
actual fun debug(msg: () -> Any?)
17+
18+
/**
19+
* Lazy add a log message if isInfoEnabled is true
20+
*/
21+
actual fun info(msg: () -> Any?)
22+
23+
/**
24+
* Lazy add a log message if isWarnEnabled is true
25+
*/
26+
actual fun warn(msg: () -> Any?)
27+
28+
/**
29+
* Lazy add a log message if isErrorEnabled is true
30+
*/
31+
actual fun error(msg: () -> Any?)
32+
33+
/**
34+
* Lazy add a log message with throwable payload if isTraceEnabled is true
35+
*/
36+
actual fun trace(t: Throwable?, msg: () -> Any?)
37+
38+
/**
39+
* Lazy add a log message with throwable payload if isDebugEnabled is true
40+
*/
41+
actual fun debug(t: Throwable?, msg: () -> Any?)
42+
43+
/**
44+
* Lazy add a log message with throwable payload if isInfoEnabled is true
45+
*/
46+
actual fun info(t: Throwable?, msg: () -> Any?)
47+
48+
/**
49+
* Lazy add a log message with throwable payload if isWarnEnabled is true
50+
*/
51+
actual fun warn(t: Throwable?, msg: () -> Any?)
52+
53+
/**
54+
* Lazy add a log message with throwable payload if isErrorEnabled is true
55+
*/
56+
actual fun error(t: Throwable?, msg: () -> Any?)
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package mu
2+
3+
import mu.internal.KLoggerJS
4+
5+
6+
actual object KotlinLogging {
7+
/**
8+
* This method allow defining the logger in a file in the following way:
9+
* val logger = KotlinLogging.logger {}
10+
*/
11+
actual fun logger(func: () -> Unit): KLogger = KLoggerJS(func::class.js.name)
12+
13+
actual fun logger(name: String): KLogger = KLoggerJS(name)
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package mu
2+
3+
var LOG_LEVEL = KotlinLoggingLevel.INFO
4+
5+
enum class KotlinLoggingLevel {
6+
TRACE,
7+
DEBUG,
8+
INFO,
9+
WARN,
10+
ERROR
11+
}
12+
13+
fun KotlinLoggingLevel.isLoggingEnabled() = this.ordinal >= LOG_LEVEL.ordinal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package mu.internal
2+
3+
import mu.KLogger
4+
import mu.KotlinLoggingLevel
5+
import mu.isLoggingEnabled
6+
7+
class KLoggerJS(private val loggerName: String) : KLogger {
8+
9+
override fun trace(msg: () -> Any?) {
10+
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) {
11+
console.log("TRACE: [$loggerName] ${msg.toStringSafe()}")
12+
}
13+
}
14+
15+
override fun debug(msg: () -> Any?) {
16+
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) {
17+
console.log("DEBUG: [$loggerName] ${msg.toStringSafe()}")
18+
}
19+
}
20+
21+
override fun info(msg: () -> Any?) {
22+
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) {
23+
console.info("INFO: [$loggerName] ${msg.toStringSafe()}")
24+
}
25+
}
26+
27+
override fun warn(msg: () -> Any?) {
28+
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) {
29+
console.warn("WARN: [$loggerName] ${msg.toStringSafe()}")
30+
}
31+
}
32+
33+
override fun error(msg: () -> Any?) {
34+
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) {
35+
console.error("ERROR: [$loggerName] ${msg.toStringSafe()}")
36+
}
37+
}
38+
39+
override fun trace(t: Throwable?, msg: () -> Any?) {
40+
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) {
41+
console.log("TRACE: [$loggerName] ${msg.toStringSafe()}${t.throwableToString()}")
42+
}
43+
}
44+
45+
override fun debug(t: Throwable?, msg: () -> Any?) {
46+
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) {
47+
console.log("DEBUG: [$loggerName] ${msg.toStringSafe()}${t.throwableToString()}")
48+
}
49+
}
50+
51+
override fun info(t: Throwable?, msg: () -> Any?) {
52+
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) {
53+
console.info("INFO: [$loggerName] ${msg.toStringSafe()}${t.throwableToString()}")
54+
}
55+
}
56+
57+
override fun warn(t: Throwable?, msg: () -> Any?) {
58+
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) {
59+
console.warn("WARN: [$loggerName] ${msg.toStringSafe()}${t.throwableToString()}")
60+
}
61+
}
62+
63+
override fun error(t: Throwable?, msg: () -> Any?) {
64+
if (KotlinLoggingLevel.TRACE.isLoggingEnabled()) {
65+
console.error("ERROR: [$loggerName] ${msg.toStringSafe()}${t.throwableToString()}")
66+
}
67+
}
68+
69+
private fun (() -> Any?).toStringSafe(): String {
70+
try {
71+
return invoke().toString()
72+
} catch (e: Exception) {
73+
return "Log message invocation failed: $e"
74+
}
75+
}
76+
77+
private fun Throwable?.throwableToString(): String {
78+
if (this == null) {
79+
return ""
80+
}
81+
var msg = ""
82+
var current = this
83+
while (current != null && current.cause != current) {
84+
msg += ", Caused by: '${current.message}'"
85+
current = current.cause
86+
}
87+
return msg
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package mu
22

3-
import mu.internal.toStringSafe
43
import org.slf4j.Logger
54

65
/**
76
* An extension for [Logger] with Lazy message evaluation
87
* example:
98
* logger.info{"this is $lazy evaluated string"}
109
*/
11-
interface KLogger : Logger {
10+
actual interface KLogger : Logger {
1211

1312
/**
1413
* The actual logger executing logging
@@ -18,70 +17,52 @@ interface KLogger : Logger {
1817
/**
1918
* Lazy add a log message if isTraceEnabled is true
2019
*/
21-
fun trace(msg: () -> Any?) {
22-
if (isTraceEnabled) trace(msg.toStringSafe())
23-
}
20+
actual fun trace(msg: () -> Any?)
2421

2522
/**
2623
* Lazy add a log message if isDebugEnabled is true
2724
*/
28-
fun debug(msg: () -> Any?) {
29-
if (isDebugEnabled) debug(msg.toStringSafe())
30-
}
25+
actual fun debug(msg: () -> Any?)
3126

3227
/**
3328
* Lazy add a log message if isInfoEnabled is true
3429
*/
35-
fun info(msg: () -> Any?) {
36-
if (isInfoEnabled) info(msg.toStringSafe())
37-
}
30+
actual fun info(msg: () -> Any?)
3831

3932
/**
4033
* Lazy add a log message if isWarnEnabled is true
4134
*/
42-
fun warn(msg: () -> Any?) {
43-
if (isWarnEnabled) warn(msg.toStringSafe())
44-
}
35+
actual fun warn(msg: () -> Any?)
4536

4637
/**
4738
* Lazy add a log message if isErrorEnabled is true
4839
*/
49-
fun error(msg: () -> Any?) {
50-
if (isErrorEnabled) error(msg.toStringSafe())
51-
}
40+
actual fun error(msg: () -> Any?)
5241

5342
/**
5443
* Lazy add a log message with throwable payload if isTraceEnabled is true
5544
*/
56-
fun trace(t: Throwable?, msg: () -> Any?) {
57-
if (isTraceEnabled) trace(msg.toStringSafe(), t)
58-
}
45+
actual fun trace(t: Throwable?, msg: () -> Any?)
5946

6047
/**
6148
* Lazy add a log message with throwable payload if isDebugEnabled is true
6249
*/
63-
fun debug(t: Throwable?, msg: () -> Any?) {
64-
if (isDebugEnabled) debug(msg.toStringSafe(), t)
65-
}
50+
actual fun debug(t: Throwable?, msg: () -> Any?)
6651

6752
/**
6853
* Lazy add a log message with throwable payload if isInfoEnabled is true
6954
*/
70-
fun info(t: Throwable?, msg: () -> Any?) {
71-
if (isInfoEnabled) info(msg.toStringSafe(), t)
72-
}
55+
actual fun info(t: Throwable?, msg: () -> Any?)
7356

7457
/**
7558
* Lazy add a log message with throwable payload if isWarnEnabled is true
7659
*/
77-
fun warn(t: Throwable?, msg: () -> Any?) {
78-
if (isWarnEnabled) warn(msg.toStringSafe(), t)
79-
}
60+
actual fun warn(t: Throwable?, msg: () -> Any?)
8061

8162
/**
8263
* Lazy add a log message with throwable payload if isErrorEnabled is true
8364
*/
84-
fun error(t: Throwable?, msg: () -> Any?) {
85-
if (isErrorEnabled) error(msg.toStringSafe(), t)
86-
}
65+
actual fun error(t: Throwable?, msg: () -> Any?)
66+
67+
8768
}

kotlin-logging-jvm/src/main/kotlin/mu/KotlinLogging.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package mu
33
import mu.internal.KLoggerFactory
44

55

6-
object KotlinLogging {
6+
actual object KotlinLogging {
77
/**
88
* This method allow defining the logger in a file in the following way:
99
* val logger = KotlinLogging.logger {}
1010
*/
11-
fun logger(func: () -> Unit): KLogger = KLoggerFactory.logger(func)
11+
actual fun logger(func: () -> Unit): KLogger = KLoggerFactory.logger(func)
1212

13-
fun logger(name: String): KLogger = KLoggerFactory.logger(name)
13+
actual fun logger(name: String): KLogger = KLoggerFactory.logger(name)
1414
}

0 commit comments

Comments
 (0)