Skip to content

Commit

Permalink
add KotlinLogging to support class level logging - a suggestion for i…
Browse files Browse the repository at this point in the history
…ssue #12 (#13)

add KotlinLogging to support class level logging
This is another flavour of how to obtain a logger that might be the preferred way it the future.
  • Loading branch information
oshai authored Dec 13, 2016
1 parent a3634bf commit 4d7be95
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
14 changes: 14 additions & 0 deletions 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.KLoggerFactory


object KotlinLogging {
/**
* This methods allow defining the logger in a file in the following way:
* val logger = KotlinLogging.logger {}
*/
fun logger(func: () -> Unit): KLogger = KLoggerFactory.logger(func)

fun logger(name: String): KLogger = KLoggerFactory.logger(name)
}
11 changes: 8 additions & 3 deletions src/main/kotlin/mu/internal/KLoggerFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@ internal object KLoggerFactory {
* get logger for the class
*/
inline internal fun logger(loggable: KLoggable): KLogger =
wrapJLogger(jLogger(KLoggerNameResolver.name(loggable.javaClass)))
logger(KLoggerNameResolver.name(loggable.javaClass))

/**
* get logger by explicit name
*/
inline internal fun logger(name: String): KLogger = wrapJLogger(jLogger(name))

/**
* get logger for the method, assuming it was declared at the logger file/class
*/
inline internal fun logger(noinline func: () -> Unit): KLogger =
logger(KLoggerNameResolver.name(func))

/**
* get a java logger by name
*/
inline internal fun jLogger(name: String): Logger = LoggerFactory.getLogger(name)
inline private fun jLogger(name: String): Logger = LoggerFactory.getLogger(name)

/**
* wrap java logger based on location awareness
Expand All @@ -37,6 +43,5 @@ internal object KLoggerFactory {
else
LocationIgnorantKLogger(jLogger)


}

15 changes: 14 additions & 1 deletion src/main/kotlin/mu/internal/KLoggerNameResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ import java.lang.reflect.Modifier
@Suppress("NOTHING_TO_INLINE")
internal object KLoggerNameResolver {

/**
* get class name for function by the package of the function
*/
inline internal fun name(noinline func: () -> Unit): String {
val name = func.javaClass.name
val slicedName = when {
name.contains("Kt$") -> name.substringBefore("Kt$")
name.contains("$") -> name.substringBefore("$")
else -> name
}
return slicedName
}

/**
* get class name for java class (that usually represents kotlin class)
*/
Expand All @@ -23,7 +36,7 @@ internal object KLoggerNameResolver {
if (clazz.enclosingClass != null) {
try {
val field = clazz.enclosingClass.getField(clazz.simpleName)
if (Modifier.isStatic(field.modifiers) && field.type == clazz ) {
if (Modifier.isStatic(field.modifiers) && field.type == clazz) {
// && field.get(null) === obj
// the above might be safer but problematic with initialization order
return clazz.enclosingClass
Expand Down
21 changes: 21 additions & 0 deletions src/test/kotlin/mu/KotlinLoggingTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package mu

import org.junit.Assert.*
import org.junit.Test

val logger = KotlinLogging.logger { }

class ForKotlinLoggingTest {
val loggerInClass = KotlinLogging.logger { }
companion object {
val loggerInCompanion = KotlinLogging.logger { }
}
}
class KotlinLoggingTest {

@Test fun testLoggerName() {
assertEquals("mu.KotlinLoggingTest", logger.name)
assertEquals("mu.ForKotlinLoggingTest", ForKotlinLoggingTest().loggerInClass.name)
assertEquals("mu.ForKotlinLoggingTest", ForKotlinLoggingTest.loggerInCompanion.name)
}
}

0 comments on commit 4d7be95

Please sign in to comment.