-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix slf4j wrapper and log field separator
- Loading branch information
Showing
9 changed files
with
139 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
klog/src/main/kotlin/com/coditory/klog/publish/InMemoryTextPublisher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.coditory.klog.publish | ||
|
||
import com.coditory.klog.LogEvent | ||
import com.coditory.klog.text.TextLogEventSerializer | ||
import com.coditory.klog.text.plain.PlainTextLogEventSerializer | ||
|
||
class InMemoryTextPublisher( | ||
private val serializer: TextLogEventSerializer = PlainTextLogEventSerializer(), | ||
) : BlockingPublisher { | ||
private val logs = mutableListOf<LogEntry>() | ||
|
||
@Synchronized | ||
override fun publishBlocking(event: LogEvent) { | ||
val sb = StringBuilder() | ||
serializer.format(event, sb) | ||
logs.add( | ||
LogEntry( | ||
event = event, | ||
message = sb.toString(), | ||
), | ||
) | ||
} | ||
|
||
@Synchronized | ||
fun getLogs(): List<String> = logs.map { it.message } | ||
|
||
@Synchronized | ||
fun getLastLog(): String? = logs.lastOrNull()?.message | ||
|
||
@Synchronized | ||
fun getLogEntries(): List<LogEntry> = logs | ||
|
||
@Synchronized | ||
fun getLastLogEntry(): LogEntry? = logs.lastOrNull() | ||
|
||
@Synchronized | ||
fun getLogEvents(): List<LogEvent> = logs.map { it.event } | ||
|
||
@Synchronized | ||
fun getLastLogEvent(): LogEvent? = logs.lastOrNull()?.event | ||
|
||
@Synchronized | ||
fun clear() { | ||
logs.clear() | ||
} | ||
|
||
data class LogEntry( | ||
val event: LogEvent, | ||
val message: String, | ||
) | ||
|
||
companion object { | ||
const val TEST_THREAD_NAME = "@THREAD" | ||
|
||
fun testPublisher(): InMemoryTextPublisher { | ||
return InMemoryTextPublisher( | ||
PlainTextLogEventSerializer( | ||
threadFormatter = { _, appendable -> appendable.append(TEST_THREAD_NAME) }, | ||
), | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
klog/src/test/kotlin/com/coditory/klog/KlogComplexPlainTextSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.coditory.klog | ||
|
||
import com.coditory.klog.publish.InMemoryTextPublisher | ||
import com.coditory.klog.publish.InMemoryTextPublisher.Companion.TEST_THREAD_NAME | ||
import com.coditory.klog.shared.UpdatableFixedClock | ||
import com.coditory.klog.shared.UpdatableFixedClock.Companion.DEFAULT_FIXED_TIME_LOG_STR | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
|
||
class KlogComplexPlainTextSpec : FunSpec({ | ||
val clock = UpdatableFixedClock() | ||
val publisher = InMemoryTextPublisher.testPublisher() | ||
val klog = | ||
klog { | ||
clock(clock) | ||
stream { | ||
blockingPublisher(publisher) | ||
} | ||
} | ||
|
||
val logger = klog.logger("com.coditory.Logger") | ||
|
||
beforeTest { | ||
publisher.clear() | ||
} | ||
|
||
test("should emit a basic info log event") { | ||
logger.info { "Hello" } | ||
publisher.getLogs().size shouldBe 1 | ||
publisher.getLastLog() shouldBe "$DEFAULT_FIXED_TIME_LOG_STR INFO $TEST_THREAD_NAME com.coditory.Logger: Hello" | ||
Check failure on line 30 in klog/src/test/kotlin/com/coditory/klog/KlogComplexPlainTextSpec.kt GitHub Actions / test reportcom.coditory.klog.KlogComplexPlainTextSpec ► should emit a basic info log event
Raw output
|
||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters