Skip to content

Commit

Permalink
Improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
saschpe committed Mar 25, 2024
1 parent 4b2a19d commit 2ccccca
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
3 changes: 3 additions & 0 deletions log4k/src/commonMain/kotlin/saschpe/log4k/ConsoleLogger.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package saschpe.log4k

/**
* Log to the platform-specific console.
*/
expect class ConsoleLogger() : Logger
21 changes: 19 additions & 2 deletions log4k/src/commonMain/kotlin/saschpe/log4k/Log.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import kotlin.native.concurrent.ThreadLocal
object Log {
enum class Level { Verbose, Debug, Info, Warning, Error, Assert }

/**
* List of logging "backends" to use, defaults to a [ConsoleLogger].
*
* You can add additional (custom) loggers, like a [FileLogger] with default parameters:
* ```kotlin
* Log.loggers += FileLogger()
* ```
*/
@JvmField
val loggers = mutableListOf<Logger>(ConsoleLogger())

Expand Down Expand Up @@ -86,5 +94,14 @@ object Log {
loggers.forEach { it.log(priority, tag, message, throwable) }
}

fun Any.logged(level: Log.Level = Log.Level.Debug) =
apply { Log.log(level, message = toString(), tag = this::class.simpleName ?: "") }
/**
* Log any Kotlin object, like:
*
* ```kotlin
* list(1,2,3).logged()
* mapOf("left" to "right").logged()
* Pair("Log4k", "rocks!").logged()
* ```
*/
inline fun <reified T : Any> T.logged(level: Log.Level = Log.Level.Debug): T =
apply { Log.log(level, message = toString(), tag = T::class.simpleName ?: "") }
8 changes: 5 additions & 3 deletions log4k/src/commonTest/kotlin/saschpe/log4k/LoggedTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package saschpe.log4k

import testing.TestLoggerTest
import kotlin.test.Test
import kotlin.test.assertEquals

internal expect val expectedListTag: String
internal expect val expectedMapTag: String
Expand All @@ -25,10 +26,11 @@ class LoggedTest : TestLoggerTest() {
val list = listOf("Hello", "World")

// Act
list.logged()
val returned: List<String> = list.logged()

// Assert
assertTestLogger(Log.Level.Debug, "[Hello, World]", expectedListTag, null)
assertTestLogger(Log.Level.Debug, "[Hello, World]", "List", null)
assertEquals(list, returned)
}

@Test
Expand All @@ -40,6 +42,6 @@ class LoggedTest : TestLoggerTest() {
map.logged()

// Assert
assertTestLogger(Log.Level.Debug, "{Hello=World}", expectedMapTag, null)
assertTestLogger(Log.Level.Debug, "{Hello=World}", "Map", null)
}
}

0 comments on commit 2ccccca

Please sign in to comment.