Skip to content

Commit

Permalink
Add Kotlin-style logging API with lambda support
Browse files Browse the repository at this point in the history
Introduced new Kotlin-style logging methods using lambda expressions, which are evaluated only if the respective log level is enabled. Updated README.md and tests to demonstrate the usage of these new methods.
  • Loading branch information
smyrgeorge committed Oct 17, 2024
1 parent 11342a7 commit be38a1f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,31 @@ Each appender may also have its own `Channel`, which is especially useful
for cases requiring batching—such as sending batched log or trace events over the network or
appending them to a file.

## API

```kotlin
log.info("this is test log")
log.info("this is test with 1 arg: {}", "hello")
log.error(e.message, e)
```

We also support a more kotlin style API:

```kotlin
log.debug { "ignore" }
log.debug { "ignore + ${5}" } // Will be evaluated only if DEBUG logs are enabled.
log.error { e.message }
log.error(e) { e.message } // e: Throwable
```

## Examples

```kotlin
// Create a Logger.
private val log: Logger = Logger.of(this::class)

log.debug("ignore")
log.debug { "ignore + ${5}" } // Will be evaluated only if DEBUG logs are enabled.
log.info("this is a test")

// Support for mute/unmute each logger programmatically.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,18 @@ abstract class Logger {
levelBeforeMute = level
}

fun trace(f: () -> String): Unit = if (Level.TRACE.shouldLog()) trace(f()) else Unit
fun trace(msg: String, vararg args: Any?): Unit = log(Level.TRACE, msg, args)
fun debug(f: () -> String): Unit = if (Level.DEBUG.shouldLog()) debug(f()) else Unit
fun debug(msg: String, vararg args: Any?): Unit = log(Level.DEBUG, msg, args)
fun info(f: () -> String): Unit = if (Level.INFO.shouldLog()) info(f()) else Unit
fun info(msg: String, vararg args: Any?): Unit = log(Level.INFO, msg, args)
fun warn(f: () -> String): Unit = if (Level.WARN.shouldLog()) warn(f()) else Unit
fun warn(msg: String, vararg args: Any?): Unit = log(Level.WARN, msg, args)
fun error(f: () -> String?): Unit = if (Level.ERROR.shouldLog()) error(f()) else Unit
fun error(msg: String?, vararg args: Any?): Unit = log(Level.ERROR, msg ?: "", args)
fun error(msg: String?, throwable: Throwable, vararg args: Any?): Unit =
log(Level.ERROR, msg ?: "", throwable, args)
fun error(t: Throwable, f: () -> String?): Unit = if (Level.ERROR.shouldLog()) error(f(), t) else Unit
fun error(msg: String?, t: Throwable, vararg args: Any?): Unit = log(Level.ERROR, msg ?: "", t, args)

companion object {
fun of(name: String): Logger = RootLogger.factory.getLogger(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class MainTests {
@Test
fun test() {
log.debug("ignore")
log.debug { "ignore + ${5}" } // Will be evaluated only if DEBUG logs are enabled.
log.info("this is a test")
RootLogger.loggers.mute("io.github.smyrgeorge.log4k.MainTests")
log.info("this is a test with 1 arg: {}", "hello")
Expand All @@ -29,7 +30,9 @@ class MainTests {
error("An error occurred!")
} catch (e: Exception) {
log.error(e.message)
log.error { e.message }
log.error(e.message, e)
log.error(e) { e.message }
}

runBlocking {
Expand Down

0 comments on commit be38a1f

Please sign in to comment.