|
1 | | -package com.httpsms |
2 | | - |
3 | | -import android.content.Context |
4 | | -import android.os.Build |
5 | | -import com.beust.klaxon.Klaxon |
6 | | -import okhttp3.MediaType.Companion.toMediaType |
7 | | -import okhttp3.OkHttpClient |
8 | | -import okhttp3.Request |
9 | | -import okhttp3.RequestBody.Companion.toRequestBody |
10 | | -import timber.log.Timber |
11 | | -import java.time.ZoneOffset |
12 | | -import java.time.ZonedDateTime |
13 | | -import java.time.format.DateTimeFormatter |
14 | | -import java.util.concurrent.ConcurrentLinkedQueue |
15 | | - |
16 | | -class LogtailTree(val context: Context): Timber.DebugTree() { |
17 | | - private val client = OkHttpClient() |
18 | | - private val jsonMediaType = "application/json; charset=utf-8".toMediaType() |
19 | | - private val queue: ConcurrentLinkedQueue<LogEntry> = ConcurrentLinkedQueue<LogEntry>() |
20 | | - |
21 | | - override fun log(priority: Int, tag: String?, message: String, t: Throwable?) { |
22 | | - val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") |
23 | | - val logEntry = LogEntry( |
24 | | - BuildConfig.VERSION_NAME, |
25 | | - priority, |
26 | | - severity(priority), |
27 | | - tag, |
28 | | - message, |
29 | | - Build.MODEL, |
30 | | - Build.BRAND, |
31 | | - Build.DEVICE, |
32 | | - Build.VERSION.SDK_INT, |
33 | | - ZonedDateTime.now(ZoneOffset.UTC).format(formatter), |
34 | | - Settings.getUserID(context), |
35 | | - t |
36 | | - ) |
37 | | - queue.add(logEntry) |
38 | | - if (queue.size < 100) { |
39 | | - return |
40 | | - } |
41 | | - |
42 | | - val logEntries = queue.toArray() |
43 | | - queue.clear() |
44 | | - |
45 | | - val request: Request = Request.Builder() |
46 | | - .url("https://in.logtail.com") |
47 | | - .post(Klaxon().toJsonString(logEntries).toRequestBody(jsonMediaType)) |
48 | | - .header("Authorization", "Bearer m7ZoA8u5KRYNe6RnEdWeZqsZ") |
49 | | - .build() |
50 | | - |
51 | | - Thread { |
52 | | - try { |
53 | | - val response = client.newCall(request).execute() |
54 | | - response.body?.close() |
55 | | - } catch(_: Exception) { |
56 | | - } |
57 | | - }.start() |
58 | | - } |
59 | | - |
60 | | - private fun severity(priority: Int): String { |
61 | | - return when(priority) { |
62 | | - 3 -> "DEBUG" |
63 | | - 4 -> "INFO" |
64 | | - 5 -> "WARNING" |
65 | | - 6 -> "ERROR" |
66 | | - 7 -> "ASSERT" |
67 | | - else -> "VERBOSE" |
68 | | - } |
69 | | - } |
70 | | - |
71 | | - class LogEntry( |
72 | | - val release: String, |
73 | | - val priority: Int, |
74 | | - val severity: String, |
75 | | - val tag: String?, |
76 | | - val message: String, |
77 | | - val model: String, |
78 | | - val brand: String, |
79 | | - val device: String, |
80 | | - val version: Int, |
81 | | - val dt: String, |
82 | | - val userID: String, |
83 | | - val throwable: Throwable?) |
84 | | -} |
| 1 | +package com.httpsms |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.os.Build |
| 5 | +import com.beust.klaxon.Json |
| 6 | +import com.beust.klaxon.Klaxon |
| 7 | +import okhttp3.MediaType.Companion.toMediaType |
| 8 | +import okhttp3.OkHttpClient |
| 9 | +import okhttp3.Request |
| 10 | +import okhttp3.RequestBody.Companion.toRequestBody |
| 11 | +import timber.log.Timber |
| 12 | +import java.time.ZoneOffset |
| 13 | +import java.time.ZonedDateTime |
| 14 | +import java.time.format.DateTimeFormatter |
| 15 | +import java.util.concurrent.ConcurrentLinkedQueue |
| 16 | + |
| 17 | +class LogzTree(val context: Context): Timber.DebugTree() { |
| 18 | + private val client = OkHttpClient() |
| 19 | + private val jsonMediaType = "application/json; charset=utf-8".toMediaType() |
| 20 | + private val queue: ConcurrentLinkedQueue<LogEntry> = ConcurrentLinkedQueue<LogEntry>() |
| 21 | + |
| 22 | + override fun log(priority: Int, tag: String?, message: String, t: Throwable?) { |
| 23 | + val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") |
| 24 | + val logEntry = LogEntry( |
| 25 | + BuildConfig.VERSION_NAME, |
| 26 | + priority, |
| 27 | + severity(priority), |
| 28 | + tag, |
| 29 | + message, |
| 30 | + Build.MODEL, |
| 31 | + Build.BRAND, |
| 32 | + Build.DEVICE, |
| 33 | + Build.VERSION.SDK_INT, |
| 34 | + ZonedDateTime.now(ZoneOffset.UTC).format(formatter), |
| 35 | + Settings.getUserID(context), |
| 36 | + t |
| 37 | + ) |
| 38 | + queue.add(logEntry) |
| 39 | + if (queue.size < 100) { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + val logEntries = queue.toArray() |
| 44 | + queue.clear() |
| 45 | + |
| 46 | + val body = logEntries.joinToString(separator = "\n") { x -> Klaxon().toJsonString(x) } |
| 47 | + .toRequestBody("application/x-www-form-urlencoded".toMediaType()) |
| 48 | + |
| 49 | + val request: Request = Request.Builder() |
| 50 | + .url("https://listener.logz.io:8071?token=cTCUVJoTDrPjaFcanAPRzIsYyThyrIDw&type=http-bulk") |
| 51 | + .post(body) |
| 52 | + .header("Content-Type", "application/x-www-form-urlencoded") |
| 53 | + .build() |
| 54 | + |
| 55 | + Thread { |
| 56 | + try { |
| 57 | + val response = client.newCall(request).execute() |
| 58 | + response.body?.close() |
| 59 | + } catch(ex: Exception) { |
| 60 | + } |
| 61 | + }.start() |
| 62 | + } |
| 63 | + |
| 64 | + private fun severity(priority: Int): String { |
| 65 | + return when(priority) { |
| 66 | + 3 -> "DEBUG" |
| 67 | + 4 -> "INFO" |
| 68 | + 5 -> "WARNING" |
| 69 | + 6 -> "ERROR" |
| 70 | + 7 -> "ASSERT" |
| 71 | + else -> "VERBOSE" |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + class LogEntry( |
| 76 | + val release: String, |
| 77 | + val priority: Int, |
| 78 | + val severity: String, |
| 79 | + val tag: String?, |
| 80 | + val message: String, |
| 81 | + val model: String, |
| 82 | + val brand: String, |
| 83 | + val device: String, |
| 84 | + val version: Int, |
| 85 | + @Json(name = "@timestamp") |
| 86 | + val dt: String, |
| 87 | + val userID: String, |
| 88 | + val throwable: Throwable?) |
| 89 | +} |
0 commit comments