Skip to content

Commit c54d670

Browse files
committed
Adding messages in a queue
1 parent 67301a6 commit c54d670

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

android/app/src/main/java/com/httpsms/LogtailTree.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import timber.log.Timber
1010
import java.time.ZoneOffset
1111
import java.time.ZonedDateTime
1212
import java.time.format.DateTimeFormatter
13+
import java.util.concurrent.ConcurrentLinkedQueue
1314

1415
class LogtailTree: Timber.DebugTree() {
1516
private val client = OkHttpClient()
1617
private val jsonMediaType = "application/json; charset=utf-8".toMediaType()
18+
private val queue: ConcurrentLinkedQueue<LogEntry> = ConcurrentLinkedQueue<LogEntry>()
1719

1820
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
19-
val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z")
20-
21+
val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z")
2122
val logEntry = LogEntry(
2223
priority,
2324
severity(priority),
@@ -30,15 +31,20 @@ class LogtailTree: Timber.DebugTree() {
3031
ZonedDateTime.now(ZoneOffset.UTC).format(formatter),
3132
t
3233
)
34+
queue.add(logEntry)
35+
if (queue.size < 10) {
36+
return
37+
}
38+
39+
val logEntries = queue.toArray()
40+
queue.clear()
3341

3442
val request: Request = Request.Builder()
3543
.url("https://in.logtail.com")
36-
.post(Klaxon().toJsonString(logEntry).toRequestBody(jsonMediaType))
44+
.post(Klaxon().toJsonString(logEntries).toRequestBody(jsonMediaType))
3745
.header("Authorization", "Bearer m7ZoA8u5KRYNe6RnEdWeZqsZ")
3846
.build()
3947

40-
41-
4248
Thread {
4349
client.newCall(request).execute()
4450
}.start()
@@ -64,6 +70,6 @@ class LogtailTree: Timber.DebugTree() {
6470
val brand: String,
6571
val device: String,
6672
val version: Int,
67-
val timestamp: String,
73+
val dt: String,
6874
val throwable: Throwable?)
69-
}
75+
}

0 commit comments

Comments
 (0)