Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for same functionality as slf4j-ext. #73

Merged
merged 1 commit into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions kotlin-logging-common/src/main/kotlin/mu/KLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,30 @@ expect interface KLogger {
* Lazy add a log message with a marker and throwable payload if isErrorEnabled is true
*/
fun error(marker: Marker?, t: Throwable?, msg: () -> Any?)

/**
* Add a log message with all the supplied parameters along with method name
*/
fun entry(vararg argArray: Any)

/**
* Add log message indicating exit of a method
*/
fun exit()

/**
* Add a log message with the return value of a method
*/
fun <T> exit(retval: T): T where T : Any

/**
* Add a log message indicating an exception will be thrown along with the stack trace.
*/
fun <T> throwing(throwable: T): T where T : Throwable

/**
* Add a log message indicating an exception is caught along with the stack trace.
*/
fun <T> catching(throwable: T) where T : Throwable

}
24 changes: 24 additions & 0 deletions kotlin-logging-js/src/main/kotlin/mu/KLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,28 @@ actual interface KLogger {
*/
actual fun error(marker: Marker?, t: Throwable?, msg: () -> Any?)

/**
* Add a log message with all the supplied parameters along with method name
*/
actual fun entry(vararg argArray: Any)

/**
* Add log message indicating exit of a method
*/
actual fun exit()

/**
* Add a log message with the return value of a method
*/
actual fun <T> exit(retval: T): T where T : Any

/**
* Add a log message indicating an exception will be thrown along with the stack trace.
*/
actual fun <T> throwing(throwable: T): T where T : Throwable

/**
* Add a log message indicating an exception is caught along with the stack trace.
*/
actual fun <T> catching(throwable: T) where T : Throwable
}
29 changes: 23 additions & 6 deletions kotlin-logging-js/src/main/kotlin/mu/internal/KLoggerJS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ import mu.KLogger
import mu.KotlinLoggingConfiguration.APPENDER
import mu.KotlinLoggingConfiguration.FORMATTER
import mu.KotlinLoggingLevel
import mu.KotlinLoggingLevel.DEBUG
import mu.KotlinLoggingLevel.ERROR
import mu.KotlinLoggingLevel.INFO
import mu.KotlinLoggingLevel.TRACE
import mu.KotlinLoggingLevel.WARN
import mu.KotlinLoggingLevel.*
import mu.Marker
import mu.isLoggingEnabled

internal class KLoggerJS(
private val loggerName: String
private val loggerName: String
) : KLogger {

override fun trace(msg: () -> Any?) = TRACE.logIfEnabled(msg, APPENDER::trace)
Expand Down Expand Up @@ -80,4 +76,25 @@ internal class KLoggerJS(
}
}

override fun entry(vararg argArray: Any) {
TRACE.logIfEnabled({ "entry($argArray)" }, APPENDER::trace)
}

override fun exit() {
TRACE.logIfEnabled({ "exit()" }, APPENDER::trace)
}

override fun <T : Any> exit(retval: T): T {
TRACE.logIfEnabled({ "exut($retval)" }, APPENDER::trace)
return retval
}

override fun <T : Throwable> throwing(throwable: T): T {
ERROR.logIfEnabled({ "throwing($throwable" }, throwable, APPENDER::error)
return throwable
}

override fun <T : Throwable> catching(throwable: T) {
ERROR.logIfEnabled({ "catching($throwable" }, throwable, APPENDER::error)
}
}
24 changes: 24 additions & 0 deletions kotlin-logging-jvm/src/main/kotlin/mu/KLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,28 @@ actual interface KLogger : Logger {
*/
actual fun error(marker: Marker?, t: Throwable?, msg: () -> Any?)

/**
* Add a log message with all the supplied parameters along with method name
*/
actual fun entry(vararg argArray: Any)

/**
* Add log message indicating exit of a method
*/
actual fun exit()

/**
* Add a log message with the return value of a method
*/
actual fun <T> exit(retval: T): T where T : Any

/**
* Add a log message indicating an exception will be thrown along with the stack trace.
*/
actual fun <T> throwing(throwable: T): T where T : Throwable

/**
* Add a log message indicating an exception is caught along with the stack trace.
*/
actual fun <T> catching(throwable: T) where T : Throwable
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mu.internal

import mu.KLogger
import mu.KMarkerFactory
import org.slf4j.Logger
import org.slf4j.Marker
import org.slf4j.helpers.MessageFormatter
Expand All @@ -13,6 +14,13 @@ import org.slf4j.spi.LocationAwareLogger
internal class LocationAwareKLogger(override val underlyingLogger: LocationAwareLogger) : KLogger, Logger by underlyingLogger {

private val fqcn: String = LocationAwareKLogger::class.java.name
private val ENTRY = KMarkerFactory.getMarker("ENTRY")
private val EXIT = KMarkerFactory.getMarker("EXIT")

private val THROWING = KMarkerFactory.getMarker("THROWING")
private val CATCHING = KMarkerFactory.getMarker("CATCHING")
private val EXITONLY = "exit"
private val EXITMESSAGE = "exit with ({})"

override fun trace(msg: String?) {
if (!underlyingLogger.isTraceEnabled)
Expand Down Expand Up @@ -613,4 +621,49 @@ internal class LocationAwareKLogger(override val underlyingLogger: LocationAware
override fun error(marker: Marker?, t: Throwable?, msg: () -> Any?) {
if (isErrorEnabled) error(marker, msg.toStringSafe(), t)
}

override fun <T : Throwable> catching(throwable: T) {
if (underlyingLogger.isErrorEnabled) {
underlyingLogger.log(CATCHING, fqcn, LocationAwareLogger.ERROR_INT, "catching", null, throwable)
}
}

override fun entry(vararg argArray: Any) {
if (underlyingLogger.isTraceEnabled(ENTRY)) {
val tp = MessageFormatter.arrayFormat(buildMessagePattern(argArray.size), argArray)
underlyingLogger.log(ENTRY, fqcn, LocationAwareLogger.TRACE_INT, tp.message, null, null);
}
}

override fun exit() {
if (underlyingLogger.isTraceEnabled(EXIT)) {
underlyingLogger.log(EXIT, fqcn, LocationAwareLogger.TRACE_INT, EXITONLY, null, null)
}
}

override fun <T: Any> exit(retval: T): T {
if (underlyingLogger.isTraceEnabled(EXIT)) {
val tp = MessageFormatter.format(EXITMESSAGE, retval)
underlyingLogger.log(EXIT, fqcn, LocationAwareLogger.TRACE_INT, tp.message, arrayOf<Any>(retval), tp.throwable)
}
return retval
}

override fun <T : Throwable> throwing(throwable: T): T {
underlyingLogger.log(THROWING, fqcn, LocationAwareLogger.ERROR_INT, "throwing", null, throwable)
throw throwable
}

private fun buildMessagePattern(len: Int): String {
val sb = StringBuilder()
sb.append(" entry with (")
for (i in 0 until len) {
sb.append("{}")
if (i != len - 1)
sb.append(", ")
}
sb.append(')')
return sb.toString()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,35 @@ internal class LocationIgnorantKLogger(override val underlyingLogger: Logger)
override fun error(marker: Marker?, t: Throwable?, msg: () -> Any?) {
if (isErrorEnabled) error(marker, msg.toStringSafe(), t)
}
override inline fun entry(vararg argArray: Any) {
if (underlyingLogger.isTraceEnabled) {
underlyingLogger.trace("entry({})", argArray)
}
}

override inline fun exit() {
if (underlyingLogger.isTraceEnabled) {
underlyingLogger.trace("exit")
}
}

override inline fun <T : Any> exit(retval: T): T {
if (underlyingLogger.isTraceEnabled) {
underlyingLogger.trace("exit({}}", retval)
}
return retval
}

override inline fun <T : Throwable> throwing(throwable: T): T {
if (underlyingLogger.isErrorEnabled) {
underlyingLogger.error("throwing($throwable)", throwable)
}
return throwable
}

override inline fun <T : Throwable> catching(throwable: T) {
if (underlyingLogger.isErrorEnabled) {
underlyingLogger.error("catching($throwable)", throwable)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ class ClassWithLoggingForLocationTesting {
fun logNull() {
logger.info(null)
}

fun logEntry() {
logger.entry(1, 2)
logger.info("log entry body")
logger.exit(2)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class LoggingWithLocationTest {
ClassWithLoggingForLocationTesting().logNull()
Assert.assertEquals("INFO ClassWithLoggingForLocationTesting.logNull(15) - null", appenderWithWriter.writer.toString().trim())
}
@Test
fun testNullLoggingWithLocationEntryExit() {
ClassWithLoggingForLocationTesting().logEntry()
Assert.assertEquals("TRACE ClassWithLoggingForLocationTesting.logEntry(19) - entry with (1, 2)\n" +
"INFO ClassWithLoggingForLocationTesting.logEntry(20) - log entry body\n" +
"TRACE ClassWithLoggingForLocationTesting.logEntry(21) - exit with (2)", appenderWithWriter.writer.toString().trim())
}
}