Emil Kantis
04/17/2023, 8:28 PMLoggingContext
which exposes functions to add log events.
context(LoggingContext)
fun logHello() {
info { "hello" }
}
My main motivation is that it would be really easy to swap the context for a recording context which can be used to easily verify logs. 🙂
Does this fill a need, or are there better ways to achieve this already?simon.vergauwen
04/17/2023, 9:06 PMEmil Kantis
04/17/2023, 9:23 PMdave08
04/18/2023, 2:11 AMEmil Kantis
04/18/2023, 4:11 AMcontext(LoggingContext)
private suspend fun processInvoice(
// ...
) {
val lockResult = acquireLock(companyId, invoice).onSuccess {
info { "Successfully acquired lock for ${invoice.invoiceNr}." }
}.onFailure {
warn { "Failed to acquire lock for ${invoice.invoiceNr}." }
}
// ...
}
Testing:
withTestLogging {
gateways.processInvoices(config, today = LocalDate.of(2022, 4, 27))
}.shouldContainExactly(
<http://LogLevel.INFO|LogLevel.INFO>("Fetched supplier null (id=99) with bankgiro 55551234"),
LogLevel.WARN("Failed to acquire lock for ${clientInvoice.invoiceNr}."),
LogLevel.WARN("Skipping invoice since DB lock was failed to acquire"),
)
dave08
04/18/2023, 6:25 AMsimon.vergauwen
04/18/2023, 6:44 AMdave08
04/18/2023, 6:49 AMwith(....) { }
blocks that one needs to provide to use them and the hacks needed to compile them is a bit scary.simon.vergauwen
04/18/2023, 6:59 AMsome have a certain scope, some lazy, some singletons that should be loaded only when needed...I find this easier to do manually than with a framework, but that is perhaps preference or familiarity.
but alreadyjust the customThis is indeed a bit cumbersome, but the boilerplate could be already pushed to a library. Part of the KEEP for context receivers mentions anblocks that one needs to providewith(....) { }
n-arity
with method but it requires some planning and engineering from the Kotlin team on how this will be put into K2.
And I still can't get one of my projects to compile with context receivers...Most problematic piece is that it's currently experimental, and extreme low priority compared to finishing K2. We should see K2 arriving this year though, so 🤞 for stabilisation soon.
Emil Kantis
04/18/2023, 7:04 AMraulraja
04/18/2023, 9:21 AMinfo { "message" }
instead of info("message")
. Is that to avoid evaluation of the message at different log levels?Emil Kantis
04/18/2023, 9:25 AM