Format timestamp in error log

This commit is contained in:
Pierre-Yves Nicolas
2025-11-26 20:00:37 +01:00
parent 8c9be73022
commit 4b103c6aea

View File

@@ -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 {