@@ -22,24 +22,64 @@ import io.realm.kotlin.log.RealmLogger
22
22
import io.sentry.Breadcrumb
23
23
import io.sentry.Sentry
24
24
import io.sentry.SentryLevel
25
+ import kotlinx.coroutines.CoroutineScope
26
+ import kotlinx.coroutines.Dispatchers
27
+ import kotlinx.coroutines.launch
28
+ import kotlinx.coroutines.sync.Mutex
29
+ import kotlinx.coroutines.sync.withLock
25
30
26
31
class SentryRealmLogger : RealmLogger {
27
32
28
- override val level: LogLevel = LogLevel .DEBUG
33
+ private val mutex = Mutex ()
34
+ private val messagesMap = mutableMapOf<Long , MutableList <String >>()
29
35
30
- override val tag: String = " Realm"
36
+ override val level = LogLevel .DEBUG
37
+ override val tag = " Realm"
31
38
32
39
override fun log (level : LogLevel , throwable : Throwable ? , message : String? , vararg args : Any? ) {
40
+
33
41
val throwableMessage = throwable?.message
34
- val breadcrumb = if (throwableMessage == null ) {
35
- Breadcrumb ().apply {
36
- this .level = SentryLevel .INFO
37
- this .category = tag
38
- this .message = " ($tag ): $message "
42
+ if (throwableMessage != null ) {
43
+ val breadcrumb = Breadcrumb .error(throwableMessage).apply { category = " exception" }
44
+ Sentry .addBreadcrumb(breadcrumb)
45
+ return
46
+ }
47
+
48
+ CoroutineScope (Dispatchers .IO ).launch {
49
+ mutex.withLock {
50
+ val now = System .currentTimeMillis() / MAX_DELAY
51
+
52
+ message?.let {
53
+ if (messagesMap[now] == null ) {
54
+ messagesMap[now] = mutableListOf (it)
55
+ } else {
56
+ messagesMap[now]?.add(it)
57
+ }
58
+ }
59
+
60
+ var shouldContinue = true
61
+ while (shouldContinue) {
62
+ val key = messagesMap.keys.firstOrNull()
63
+ val values = messagesMap[key]
64
+ if (key != null && (key < now || values!! .count() > MAX_ZIP )) {
65
+ val breadcrumb = Breadcrumb ().apply {
66
+ this .level = SentryLevel .INFO
67
+ this .category = tag
68
+ this .message = values?.joinToString(separator = " \n " ) { it }
69
+ }
70
+ Sentry .addBreadcrumb(breadcrumb)
71
+ messagesMap.remove(key)
72
+
73
+ } else {
74
+ shouldContinue = false
75
+ }
76
+ }
39
77
}
40
- } else {
41
- Breadcrumb .error(throwableMessage).apply { category = " exception" }
42
78
}
43
- Sentry .addBreadcrumb(breadcrumb)
79
+ }
80
+
81
+ companion object {
82
+ private const val MAX_DELAY = 100
83
+ private const val MAX_ZIP = 100
44
84
}
45
85
}
0 commit comments