Logging
This commit is contained in:
1 parent
39960093fc
commit
a2be8e127a
2 files changed
+90
-7
No files matched your search
@@ -4,6 +4,12 @@ import mu.KotlinLogging
|
||||
import no.iktdev.eventi.models.DeleteEvent
|
||||
import no.iktdev.eventi.models.Event
|
||||
import no.iktdev.eventi.models.SignalEvent
|
||||
import no.iktdev.eventi.models.diagnostics.DispatchLog
|
||||
import no.iktdev.eventi.models.diagnostics.accept
|
||||
import no.iktdev.eventi.models.diagnostics.error
|
||||
import no.iktdev.eventi.models.diagnostics.ignored
|
||||
import no.iktdev.eventi.models.diagnostics.rejected
|
||||
import no.iktdev.eventi.models.diagnostics.skipped
|
||||
import no.iktdev.eventi.registry.EventListenerRegistry
|
||||
import no.iktdev.eventi.stores.EventStore
|
||||
import java.util.UUID
|
||||
@@ -24,8 +30,9 @@ open class EventDispatcher(val eventStore: EventStore) {
|
||||
.filterNot { it is DeleteEvent }
|
||||
|
||||
EventListenerRegistry.getListeners().forEach { listener ->
|
||||
var dispatchLog: MutableList<DispatchLog> = mutableListOf()
|
||||
for (candidate in candidates) {
|
||||
log.debug("Evaluating candidate: ${candidate::class.simpleName} for listener ${listener::class.simpleName}")
|
||||
//log.debug("Evaluating candidate: ${candidate::class.simpleName} for listener ${listener::class.simpleName}")
|
||||
try {
|
||||
val result = listener.onEvent(candidate, effectiveHistory)
|
||||
|
||||
@@ -33,22 +40,36 @@ open class EventDispatcher(val eventStore: EventStore) {
|
||||
validateReferenceId(result, listener)
|
||||
validateDerivation(result, candidate, effectiveHistory, listener)
|
||||
eventStore.persist(result)
|
||||
dispatchLog.accept(candidate, listener)
|
||||
} else {
|
||||
dispatchLog.ignored(candidate, listener)
|
||||
}
|
||||
|
||||
} catch (e: SoftDispatchException.UnqualifiedEntryEventException) {
|
||||
log.debug("Skipped: ${listener::class.simpleName} ignored ${candidate::class.simpleName} → ${e.message}")
|
||||
dispatchLog.rejected(candidate, listener, e.message)
|
||||
} catch (e: SoftDispatchException.SkipListenerException) {
|
||||
log.debug("Skipped: ${listener::class.simpleName} skipped ${candidate::class.simpleName} → ${e.message}")
|
||||
dispatchLog.skipped(candidate, listener, e.message)
|
||||
} catch (e: SoftDispatchException) {
|
||||
log.warn(
|
||||
"Skipped: ${listener::class.simpleName} rejected ${candidate::class.simpleName} → ${e.message}"
|
||||
)
|
||||
|
||||
dispatchLog.error(candidate, listener, e.message)
|
||||
}
|
||||
printLog(dispatchLog)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun printLog(dispatchLog: List<DispatchLog>) {
|
||||
val first = dispatchLog.first()
|
||||
var output: String = "[${first.event.referenceId}] @ [${first.event.eventId}] → ${first.event::class.java.name}\n"
|
||||
dispatchLog.forEach { it ->
|
||||
output += "\t${it.getTypeStyled()}\t ${it.listener::class.java.name}\t"
|
||||
if (!it.message.isNullOrBlank()) {
|
||||
output += it.message
|
||||
}
|
||||
output += "\n"
|
||||
}
|
||||
log.debug(output)
|
||||
}
|
||||
|
||||
fun List<Event>.replayCandidates(): List<Event> {
|
||||
val derivedFromIds = this
|
||||
.filterNot { it is SignalEvent } // ignorer signaler som “forbrukere”
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package no.iktdev.eventi.models.diagnostics
|
||||
|
||||
import no.iktdev.eventi.events.EventListener
|
||||
import no.iktdev.eventi.models.Event
|
||||
|
||||
enum class LogType {
|
||||
Accepted,
|
||||
Rejected,
|
||||
Ignored,
|
||||
Skipped,
|
||||
Error
|
||||
}
|
||||
|
||||
private const val RESET = "\u001B[0m"
|
||||
private const val RED = "\u001B[31m"
|
||||
private const val GREEN = "\u001B[32m"
|
||||
private const val YELLOW = "\u001B[33m"
|
||||
private const val BLUE = "\u001B[34m"
|
||||
private const val CYAN = "\u001B[36m"
|
||||
private const val MAGENTA = "\u001B[35m"
|
||||
|
||||
|
||||
fun colorFor(type: LogType): String = when (type) {
|
||||
LogType.Accepted -> GREEN
|
||||
LogType.Ignored -> YELLOW
|
||||
LogType.Skipped -> CYAN
|
||||
LogType.Rejected -> RED
|
||||
LogType.Error -> MAGENTA
|
||||
}
|
||||
|
||||
|
||||
data class DispatchLog(
|
||||
val event: Event,
|
||||
val listener: EventListener,
|
||||
val type: LogType,
|
||||
val message: String? = null
|
||||
) {
|
||||
fun getTypeStyled(): String {
|
||||
val color = colorFor(type)
|
||||
return "$color[${type.name}]$RESET"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun MutableList<DispatchLog>.accept(event: Event, listener: EventListener) {
|
||||
this.add(DispatchLog(event, listener, LogType.Accepted))
|
||||
}
|
||||
|
||||
fun MutableList<DispatchLog>.ignored(event: Event, listener: EventListener, message: String? = null) {
|
||||
this.add(DispatchLog(event, listener, LogType.Ignored, message))
|
||||
}
|
||||
|
||||
fun MutableList<DispatchLog>.rejected(event: Event, listener: EventListener, message: String? = null) {
|
||||
this.add(DispatchLog(event, listener, LogType.Rejected, message))
|
||||
}
|
||||
fun MutableList<DispatchLog>.skipped(event: Event, listener: EventListener, message: String? = null) {
|
||||
this.add(DispatchLog(event, listener, LogType.Skipped, message))
|
||||
}
|
||||
fun MutableList<DispatchLog>.error(event: Event, listener: EventListener, message: String? = null) {
|
||||
this.add(DispatchLog(event, listener, LogType.Error, message))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user