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

Testing for NONE log level #42

Merged
merged 6 commits into from Aug 18, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/commonMain/kotlin/io/klogging/BaseLogger.kt
Expand Up @@ -22,6 +22,7 @@ import io.klogging.Level.DEBUG
import io.klogging.Level.ERROR
import io.klogging.Level.FATAL
import io.klogging.Level.INFO
import io.klogging.Level.NONE
import io.klogging.Level.TRACE
import io.klogging.Level.WARN
import io.klogging.internal.KloggingState
Expand All @@ -45,7 +46,10 @@ public interface BaseLogger {
* Check whether this logger will emit log events at the specified logging
* level.
*/
public fun isLevelEnabled(level: Level): Boolean = minLevel() <= level
public fun isLevelEnabled(level: Level): Boolean = when {
NONE == level -> false
else -> minLevel() <= level
}

/** Is this logger enabled to emit [TRACE] events? */
public fun isTraceEnabled(): Boolean = isLevelEnabled(TRACE)
Expand Down
22 changes: 16 additions & 6 deletions src/commonMain/kotlin/io/klogging/Klogger.kt
Expand Up @@ -34,31 +34,41 @@ public interface Klogger : BaseLogger {
/** Emit an event after minimum level checking. */
public suspend fun emitEvent(level: Level, exception: Exception?, event: Any?)

public suspend fun log(level: Level, exception: Exception, event: Any?): Unit =
public suspend fun log(level: Level, exception: Exception, event: Any?) {
if (!isLevelEnabled(level)) return
emitEvent(level, exception, event)
}

public suspend fun log(level: Level, event: Any?): Unit =
public suspend fun log(level: Level, event: Any?) {
if (!isLevelEnabled(level)) return
emitEvent(level, null, event)
}

public suspend fun log(
level: Level,
exception: Exception,
template: String,
vararg values: Any?
): Unit =
) {
if (!isLevelEnabled(level)) return
if (values.isEmpty()) emitEvent(level, exception, template)
else emitEvent(level, exception, e(template, *values))
}

public suspend fun log(level: Level, template: String, vararg values: Any?): Unit =
public suspend fun log(level: Level, template: String, vararg values: Any?) {
if (!isLevelEnabled(level)) return
if (values.isEmpty()) emitEvent(level, null, template)
else emitEvent(level, null, e(template, *values))
}

public suspend fun log(level: Level, exception: Exception, event: suspend Klogger.() -> Any?) {
if (isLevelEnabled(level)) emitEvent(level, exception, event())
if (!isLevelEnabled(level)) return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the original is clearer. An early return is only needed if what follows is more complex.

But I won’t go to the barricades over this. I can see the consistency with the earlier, more complex functions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, two reasons:

  1. Consistency -- as noted
  2. Personal preference. I like to see early return. It's like a "pre condition", so I know what follows is the real work

emitEvent(level, exception, event())
}

public suspend fun log(level: Level, event: suspend Klogger.() -> Any?) {
if (isLevelEnabled(level)) emitEvent(level, null, event())
if (!isLevelEnabled(level)) return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

emitEvent(level, null, event())
}

public suspend fun trace(event: Any?): Unit = log(TRACE, event)
Expand Down
49 changes: 38 additions & 11 deletions src/jvmTest/kotlin/io/klogging/KloggerTest.kt
Expand Up @@ -31,9 +31,9 @@ import io.klogging.events.timestampNow
import io.klogging.template.templateItems
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.maps.shouldContain
import io.kotest.matchers.nulls.shouldBeNull
import io.kotest.matchers.shouldBe
import java.lang.Thread.currentThread
import kotlin.time.Duration
import kotlin.time.ExperimentalTime

class TestLogger(private val minLevel: Level = TRACE) : Klogger {
Expand Down Expand Up @@ -68,8 +68,38 @@ class TestException(message: String) : Exception(message)
class KloggerTest : DescribeSpec({
// Capture once rather than call several times: Avoid flaky tests if the OS sleeps our process
val now = timestampNow()
val thing = object {
override fun toString() = "foo"
}

describe("KtLogger") {
describe("does not log when level is NONE") {
it("for a string message") {
with(TestLogger()) {
log(NONE, "foo")
logged.shouldBeNull()
}
}
it("for a string message with an exception") {
with(TestLogger()) {
log(NONE, TestException("low bar"), "foo")
logged.shouldBeNull()
}
}
it("for an object") {
with(TestLogger()) {
log(NONE, thing)
logged.shouldBeNull()
}
}
it("for an object with an exception") {
with(TestLogger()) {
log(NONE, TestException("low bar"), thing)
logged.shouldBeNull()
}
}
}

describe("for different logging styles") {
it("logs a string message") {
val message = randomString()
Expand All @@ -87,14 +117,14 @@ class KloggerTest : DescribeSpec({
logged shouldBe message
}
}
it("logs a string message in a lambda") {
it("logs a string message in a code block") {
val message = randomString()
with(TestLogger()) {
info { message }
logged shouldBe message
}
}
it("logs a string message in a lambda with an exception") {
it("logs a string message in a code block with an exception") {
val message = randomString()
val exception = TestException(randomString())
with(TestLogger()) {
Expand All @@ -105,28 +135,25 @@ class KloggerTest : DescribeSpec({
}
it("logs an object") {
with(TestLogger()) {
log(DEBUG, now)
logged shouldBe now
log(DEBUG, thing)
logged shouldBe thing
}
}
it("logs an object with an exception") {
val thing = listOf(randomString(), randomString())
val exception = TestException(randomString())
with(TestLogger()) {
log(WARN, exception, thing)
except shouldBe exception
logged shouldBe thing
}
}
it("logs an object in a lambda") {
val thing = randomString() to now
it("logs an object in a code block") {
with(TestLogger()) {
info { thing }
logged shouldBe thing
}
}
it("logs an object in a lambda with an exception") {
val thing = setOf(now - Duration.seconds(5), now)
val exception = TestException(randomString())
with(TestLogger()) {
error(exception) { thing }
Expand Down Expand Up @@ -162,7 +189,7 @@ class KloggerTest : DescribeSpec({
}
}
}
it("logs a templated event using `e()` function in a lambda") {
it("logs a templated event using `e()` function in a code block") {
val template = "Id is {Id}"
val id = randomString()
with(TestLogger()) {
Expand All @@ -174,7 +201,7 @@ class KloggerTest : DescribeSpec({
}
}
}
it("logs a templated event using `e()` function in a lambda with an exception") {
it("logs a templated event using `e()` function in a code block with an exception") {
val id = randomString()
val exception = TestException(randomString())
with(TestLogger()) {
Expand Down
39 changes: 22 additions & 17 deletions src/jvmTest/kotlin/io/klogging/LevelsTest.kt
Expand Up @@ -20,9 +20,30 @@ package io.klogging

import io.klogging.events.LogEvent
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.nulls.shouldBeNull
import io.kotest.matchers.shouldBe

class LevelsTestLogger(private val level: Level) : Klogger {
internal class LevelsTest : DescribeSpec({
describe("at all logger levels") {
it("`log()` calls `logMessage()` for all levels") {
Level.values().forEach { loggerLevel ->
val logger = LevelsTestLogger(loggerLevel)
Level.values().forEach { eventLevel ->
randomString().let { message ->
logger.log(eventLevel, message)

if (logger.isLevelEnabled(eventLevel))
logger.loggedMessage shouldBe message
else
logger.loggedMessage.shouldBeNull()
}
}
}
}
}
})

private class LevelsTestLogger(private val level: Level) : Klogger {
override val name = "LevelsTestLogger"

override fun minLevel() = level
Expand All @@ -37,19 +58,3 @@ class LevelsTestLogger(private val level: Level) : Klogger {
TODO("Not yet implemented")
}
}

class LevelsTest : DescribeSpec({
describe("at all logger levels") {
it("`log()` calls `logMessage()` for all levels") {
Level.values().forEach { loggerLevel ->
val logger = LevelsTestLogger(loggerLevel)
Level.values().forEach { eventLevel ->
randomString().let { msg ->
logger.log(eventLevel, msg)
logger.loggedMessage shouldBe msg
}
}
}
}
}
})