Format timestamp in error log
This commit is contained in:
@@ -15,15 +15,23 @@
|
|||||||
package org.fairscan.app.data
|
package org.fairscan.app.data
|
||||||
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.time.Instant
|
||||||
|
import java.time.ZoneId
|
||||||
|
import java.time.format.DateTimeFormatter
|
||||||
|
|
||||||
class LogRepository(private val file: File) {
|
class LogRepository(private val file: File) {
|
||||||
|
|
||||||
|
private val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")
|
||||||
|
.withZone(ZoneId.systemDefault())
|
||||||
|
|
||||||
fun getLogs(): String = if (file.exists()) file.readText() else ""
|
fun getLogs(): String = if (file.exists()) file.readText() else ""
|
||||||
|
|
||||||
fun log(tag: String, message: String, throwable: Throwable) {
|
fun log(tag: String, message: String, throwable: Throwable) {
|
||||||
|
val timestamp = formatter.format(Instant.now())
|
||||||
|
|
||||||
val line = buildString {
|
val line = buildString {
|
||||||
append("${System.currentTimeMillis()} [$tag] $message")
|
append("$timestamp [$tag] $message\n")
|
||||||
append("\n${throwable.stackTraceToString()}")
|
append(throwable.stackTraceToString())
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user