generated from yumemi-inc/multiplatform-library-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLoggingMiddleware.kt
48 lines (40 loc) · 1.73 KB
/
LoggingMiddleware.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package io.yumemi.tart.logging
import io.yumemi.tart.core.Action
import io.yumemi.tart.core.Event
import io.yumemi.tart.core.Middleware
import io.yumemi.tart.core.State
import io.yumemi.tart.core.Store
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.launch
import kotlin.coroutines.CoroutineContext
@Suppress("unused")
open class LoggingMiddleware<S : State, A : Action, E : Event>(
private val logger: Logger = TartLogger(),
private val tag: String = "Tart",
private val severity: Logger.Severity = Logger.Severity.Debug,
) : Middleware<S, A, E> {
private lateinit var coroutineScope: CoroutineScope
final override suspend fun onInit(store: Store<S, A, E>, coroutineContext: CoroutineContext) {
this.coroutineScope = CoroutineScope(coroutineContext + Dispatchers.IO)
}
override suspend fun beforeActionDispatch(state: S, action: A) {
log { "Action: $action" }
}
override suspend fun beforeEventEmit(state: S, event: E) {
log { "Event: $event" }
}
override suspend fun beforeStateChange(state: S, nextState: S) {
log { "State: $nextState <- $state" }
}
override suspend fun beforeError(state: S, error: Throwable) {
log(throwable = error) { "Error: $error" }
}
@Suppress("MemberVisibilityCanBePrivate")
protected fun log(severity: Logger.Severity = this.severity, tag: String = this.tag, throwable: Throwable? = null, message: () -> String) {
coroutineScope.launch { // launch Coroutines to avoid blocking Store processing in case of heavy logging
logger.log(severity = severity, tag = tag, throwable = throwable, message = message())
}
}
}