forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogtailTree.kt
More file actions
78 lines (70 loc) · 2.33 KB
/
LogtailTree.kt
File metadata and controls
78 lines (70 loc) · 2.33 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.httpsms
import android.os.Build
import com.beust.klaxon.Klaxon
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import timber.log.Timber
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.util.concurrent.ConcurrentLinkedQueue
class LogtailTree: Timber.DebugTree() {
private val client = OkHttpClient()
private val jsonMediaType = "application/json; charset=utf-8".toMediaType()
private val queue: ConcurrentLinkedQueue<LogEntry> = ConcurrentLinkedQueue<LogEntry>()
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
val logEntry = LogEntry(
priority,
severity(priority),
tag,
message,
Build.MODEL,
Build.BRAND,
Build.DEVICE,
Build.VERSION.SDK_INT,
ZonedDateTime.now(ZoneOffset.UTC).format(formatter),
t
)
queue.add(logEntry)
if (queue.size < 10) {
return
}
val logEntries = queue.toArray()
queue.clear()
val request: Request = Request.Builder()
.url("https://in.logtail.com")
.post(Klaxon().toJsonString(logEntries).toRequestBody(jsonMediaType))
.header("Authorization", "Bearer m7ZoA8u5KRYNe6RnEdWeZqsZ")
.build()
Thread {
try {
client.newCall(request).execute()
} catch(ex: Exception) {
}
}.start()
}
private fun severity(priority: Int): String {
return when(priority) {
3 -> "DEBUG"
4 -> "INFO"
5 -> "WARNING"
6 -> "ERROR"
7 -> "ASSERT"
else -> "VERBOSE"
}
}
class LogEntry(
val priority: Int,
val severity: String,
val tag: String?,
val message: String,
val model: String,
val brand: String,
val device: String,
val version: Int,
val dt: String,
val throwable: Throwable?)
}