Skip to content

Commit a933f42

Browse files
corneiloshai
authored andcommitted
added support for same functionality as slf4j-ext.
1 parent 6688f02 commit a933f42

File tree

8 files changed

+194
-6
lines changed

8 files changed

+194
-6
lines changed

kotlin-logging-common/src/main/kotlin/mu/KLogger.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,30 @@ expect interface KLogger {
104104
* Lazy add a log message with a marker and throwable payload if isErrorEnabled is true
105105
*/
106106
fun error(marker: Marker?, t: Throwable?, msg: () -> Any?)
107+
108+
/**
109+
* Add a log message with all the supplied parameters along with method name
110+
*/
111+
fun entry(vararg argArray: Any)
112+
113+
/**
114+
* Add log message indicating exit of a method
115+
*/
116+
fun exit()
117+
118+
/**
119+
* Add a log message with the return value of a method
120+
*/
121+
fun <T> exit(retval: T): T where T : Any
122+
123+
/**
124+
* Add a log message indicating an exception will be thrown along with the stack trace.
125+
*/
126+
fun <T> throwing(throwable: T): T where T : Throwable
127+
128+
/**
129+
* Add a log message indicating an exception is caught along with the stack trace.
130+
*/
131+
fun <T> catching(throwable: T) where T : Throwable
132+
107133
}

kotlin-logging-js/src/main/kotlin/mu/KLogger.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,28 @@ actual interface KLogger {
105105
*/
106106
actual fun error(marker: Marker?, t: Throwable?, msg: () -> Any?)
107107

108+
/**
109+
* Add a log message with all the supplied parameters along with method name
110+
*/
111+
actual fun entry(vararg argArray: Any)
112+
113+
/**
114+
* Add log message indicating exit of a method
115+
*/
116+
actual fun exit()
117+
118+
/**
119+
* Add a log message with the return value of a method
120+
*/
121+
actual fun <T> exit(retval: T): T where T : Any
122+
123+
/**
124+
* Add a log message indicating an exception will be thrown along with the stack trace.
125+
*/
126+
actual fun <T> throwing(throwable: T): T where T : Throwable
127+
128+
/**
129+
* Add a log message indicating an exception is caught along with the stack trace.
130+
*/
131+
actual fun <T> catching(throwable: T) where T : Throwable
108132
}

kotlin-logging-js/src/main/kotlin/mu/internal/KLoggerJS.kt

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ import mu.KLogger
44
import mu.KotlinLoggingConfiguration.APPENDER
55
import mu.KotlinLoggingConfiguration.FORMATTER
66
import mu.KotlinLoggingLevel
7-
import mu.KotlinLoggingLevel.DEBUG
8-
import mu.KotlinLoggingLevel.ERROR
9-
import mu.KotlinLoggingLevel.INFO
10-
import mu.KotlinLoggingLevel.TRACE
11-
import mu.KotlinLoggingLevel.WARN
7+
import mu.KotlinLoggingLevel.*
128
import mu.Marker
139
import mu.isLoggingEnabled
1410

1511
internal class KLoggerJS(
16-
private val loggerName: String
12+
private val loggerName: String
1713
) : KLogger {
1814

1915
override fun trace(msg: () -> Any?) = TRACE.logIfEnabled(msg, APPENDER::trace)
@@ -80,4 +76,25 @@ internal class KLoggerJS(
8076
}
8177
}
8278

79+
override fun entry(vararg argArray: Any) {
80+
TRACE.logIfEnabled({ "entry($argArray)" }, APPENDER::trace)
81+
}
82+
83+
override fun exit() {
84+
TRACE.logIfEnabled({ "exit()" }, APPENDER::trace)
85+
}
86+
87+
override fun <T : Any> exit(retval: T): T {
88+
TRACE.logIfEnabled({ "exut($retval)" }, APPENDER::trace)
89+
return retval
90+
}
91+
92+
override fun <T : Throwable> throwing(throwable: T): T {
93+
ERROR.logIfEnabled({ "throwing($throwable" }, throwable, APPENDER::error)
94+
return throwable
95+
}
96+
97+
override fun <T : Throwable> catching(throwable: T) {
98+
ERROR.logIfEnabled({ "catching($throwable" }, throwable, APPENDER::error)
99+
}
83100
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,28 @@ actual interface KLogger : Logger {
114114
*/
115115
actual fun error(marker: Marker?, t: Throwable?, msg: () -> Any?)
116116

117+
/**
118+
* Add a log message with all the supplied parameters along with method name
119+
*/
120+
actual fun entry(vararg argArray: Any)
121+
122+
/**
123+
* Add log message indicating exit of a method
124+
*/
125+
actual fun exit()
126+
127+
/**
128+
* Add a log message with the return value of a method
129+
*/
130+
actual fun <T> exit(retval: T): T where T : Any
131+
132+
/**
133+
* Add a log message indicating an exception will be thrown along with the stack trace.
134+
*/
135+
actual fun <T> throwing(throwable: T): T where T : Throwable
136+
137+
/**
138+
* Add a log message indicating an exception is caught along with the stack trace.
139+
*/
140+
actual fun <T> catching(throwable: T) where T : Throwable
117141
}

kotlin-logging-jvm/src/main/kotlin/mu/internal/LocationAwareKLogger.kt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mu.internal
22

33
import mu.KLogger
4+
import mu.KMarkerFactory
45
import org.slf4j.Logger
56
import org.slf4j.Marker
67
import org.slf4j.helpers.MessageFormatter
@@ -13,6 +14,13 @@ import org.slf4j.spi.LocationAwareLogger
1314
internal class LocationAwareKLogger(override val underlyingLogger: LocationAwareLogger) : KLogger, Logger by underlyingLogger {
1415

1516
private val fqcn: String = LocationAwareKLogger::class.java.name
17+
private val ENTRY = KMarkerFactory.getMarker("ENTRY")
18+
private val EXIT = KMarkerFactory.getMarker("EXIT")
19+
20+
private val THROWING = KMarkerFactory.getMarker("THROWING")
21+
private val CATCHING = KMarkerFactory.getMarker("CATCHING")
22+
private val EXITONLY = "exit"
23+
private val EXITMESSAGE = "exit with ({})"
1624

1725
override fun trace(msg: String?) {
1826
if (!underlyingLogger.isTraceEnabled)
@@ -613,4 +621,49 @@ internal class LocationAwareKLogger(override val underlyingLogger: LocationAware
613621
override fun error(marker: Marker?, t: Throwable?, msg: () -> Any?) {
614622
if (isErrorEnabled) error(marker, msg.toStringSafe(), t)
615623
}
624+
625+
override fun <T : Throwable> catching(throwable: T) {
626+
if (underlyingLogger.isErrorEnabled) {
627+
underlyingLogger.log(CATCHING, fqcn, LocationAwareLogger.ERROR_INT, "catching", null, throwable)
628+
}
629+
}
630+
631+
override fun entry(vararg argArray: Any) {
632+
if (underlyingLogger.isTraceEnabled(ENTRY)) {
633+
val tp = MessageFormatter.arrayFormat(buildMessagePattern(argArray.size), argArray)
634+
underlyingLogger.log(ENTRY, fqcn, LocationAwareLogger.TRACE_INT, tp.message, null, null);
635+
}
636+
}
637+
638+
override fun exit() {
639+
if (underlyingLogger.isTraceEnabled(EXIT)) {
640+
underlyingLogger.log(EXIT, fqcn, LocationAwareLogger.TRACE_INT, EXITONLY, null, null)
641+
}
642+
}
643+
644+
override fun <T: Any> exit(retval: T): T {
645+
if (underlyingLogger.isTraceEnabled(EXIT)) {
646+
val tp = MessageFormatter.format(EXITMESSAGE, retval)
647+
underlyingLogger.log(EXIT, fqcn, LocationAwareLogger.TRACE_INT, tp.message, arrayOf<Any>(retval), tp.throwable)
648+
}
649+
return retval
650+
}
651+
652+
override fun <T : Throwable> throwing(throwable: T): T {
653+
underlyingLogger.log(THROWING, fqcn, LocationAwareLogger.ERROR_INT, "throwing", null, throwable)
654+
throw throwable
655+
}
656+
657+
private fun buildMessagePattern(len: Int): String {
658+
val sb = StringBuilder()
659+
sb.append(" entry with (")
660+
for (i in 0 until len) {
661+
sb.append("{}")
662+
if (i != len - 1)
663+
sb.append(", ")
664+
}
665+
sb.append(')')
666+
return sb.toString()
667+
}
668+
616669
}

kotlin-logging-jvm/src/main/kotlin/mu/internal/LocationIgnorantKLogger.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,35 @@ internal class LocationIgnorantKLogger(override val underlyingLogger: Logger)
152152
override fun error(marker: Marker?, t: Throwable?, msg: () -> Any?) {
153153
if (isErrorEnabled) error(marker, msg.toStringSafe(), t)
154154
}
155+
override inline fun entry(vararg argArray: Any) {
156+
if (underlyingLogger.isTraceEnabled) {
157+
underlyingLogger.trace("entry({})", argArray)
158+
}
159+
}
160+
161+
override inline fun exit() {
162+
if (underlyingLogger.isTraceEnabled) {
163+
underlyingLogger.trace("exit")
164+
}
165+
}
166+
167+
override inline fun <T : Any> exit(retval: T): T {
168+
if (underlyingLogger.isTraceEnabled) {
169+
underlyingLogger.trace("exit({}}", retval)
170+
}
171+
return retval
172+
}
173+
174+
override inline fun <T : Throwable> throwing(throwable: T): T {
175+
if (underlyingLogger.isErrorEnabled) {
176+
underlyingLogger.error("throwing($throwable)", throwable)
177+
}
178+
return throwable
179+
}
180+
181+
override inline fun <T : Throwable> catching(throwable: T) {
182+
if (underlyingLogger.isErrorEnabled) {
183+
underlyingLogger.error("catching($throwable)", throwable)
184+
}
185+
}
155186
}

kotlin-logging-jvm/src/test/kotlin/mu/ClassWithLoggingForLocationTesting.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ class ClassWithLoggingForLocationTesting {
1414
fun logNull() {
1515
logger.info(null)
1616
}
17+
18+
fun logEntry() {
19+
logger.entry(1, 2)
20+
logger.info("log entry body")
21+
logger.exit(2)
22+
}
1723
}

kotlin-logging-jvm/src/test/kotlin/mu/LoggingWithLocationTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ class LoggingWithLocationTest {
3333
ClassWithLoggingForLocationTesting().logNull()
3434
Assert.assertEquals("INFO ClassWithLoggingForLocationTesting.logNull(15) - null", appenderWithWriter.writer.toString().trim())
3535
}
36+
@Test
37+
fun testNullLoggingWithLocationEntryExit() {
38+
ClassWithLoggingForLocationTesting().logEntry()
39+
Assert.assertEquals("TRACE ClassWithLoggingForLocationTesting.logEntry(19) - entry with (1, 2)\n" +
40+
"INFO ClassWithLoggingForLocationTesting.logEntry(20) - log entry body\n" +
41+
"TRACE ClassWithLoggingForLocationTesting.logEntry(21) - exit with (2)", appenderWithWriter.writer.toString().trim())
42+
}
3643
}
3744

3845

0 commit comments

Comments
 (0)