feat(logging): enhance log entry handling with truncation and improve upload file naming

This commit is contained in:
2026-05-14 18:23:21 +02:00
parent 063f2fd5ca
commit c70657e627
2 changed files with 18 additions and 8 deletions

View File

@@ -122,7 +122,7 @@ private class LogFileStore(
}
private fun buildEntry(priority: Int, tag: String?, message: String, throwable: Throwable?): String {
return buildString {
val entry = buildString {
append(timestampFormatter.format(LocalDateTime.now()))
append(' ')
append(priorityLabel(priority))
@@ -138,6 +138,7 @@ private class LogFileStore(
append('\n')
}
}
return entry.truncateForLog()
}
private fun priorityLabel(priority: Int): String = when (priority) {
@@ -174,11 +175,7 @@ private class LogFileStore(
tempFile
}
return tempFiles.mapIndexed { index, file ->
val uploadName = if (tempFiles.size == 1) {
"$uploadTimestamp.log"
} else {
"${uploadTimestamp}_${(index + 1).toString().padStart(3, '0')}.log"
}
val uploadName = "${uploadTimestamp}_${(index + 1).toString().padStart(3, '0')}.log"
val uploadFile = File(directory, uploadName)
uploadFile.delete()
if (!file.renameTo(uploadFile)) {
@@ -188,6 +185,14 @@ private class LogFileStore(
}
}
private fun String.truncateForLog(): String {
if (length <= MAX_LOG_ENTRY_CHARS) {
return this
}
val omittedChars = length - MAX_LOG_ENTRY_CHARS
return take(MAX_LOG_ENTRY_CHARS) + "\n... truncated $omittedChars more chars\n"
}
private fun uploadFiles(): List<File> {
return directory.listFiles()
.orEmpty()
@@ -207,7 +212,8 @@ private class LogFileStore(
private companion object {
const val ACTIVE_LOG_FILE = "purefin.log"
const val MAX_LOG_BYTES = 512 * 1024
const val MAX_LOG_BYTES = 5 * 1024 * 1024
const val MAX_LOG_ENTRY_CHARS = 4_000
const val MAX_LOG_FILES = 4
val UPLOAD_LOG_FILE_REGEX = Regex("""\d{8}_\d{6}(?:_\d{3})?\.log""")
}