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

View File

@@ -43,7 +43,7 @@ class LogUploadSettingsProvider @Inject constructor(
} }
logFiles.forEach { logFile -> logFiles.forEach { logFile ->
val uploadedName = jellyfinApiClient.uploadLogFile(logFile.data) val uploadedName = jellyfinApiClient.uploadLogFile(logFile.contentForUpload())
?: error("Log upload failed") ?: error("Log upload failed")
PurefinLogger.deleteUploadedFile(logFile) PurefinLogger.deleteUploadedFile(logFile)
Timber.tag(TAG).d("Uploaded log file ${logFile.name} as $uploadedName and deleted it locally") Timber.tag(TAG).d("Uploaded log file ${logFile.name} as $uploadedName and deleted it locally")
@@ -51,6 +51,10 @@ class LogUploadSettingsProvider @Inject constructor(
} }
} }
private fun hu.bbara.purefin.core.logging.UploadLogFile.contentForUpload(): String {
return "Purefin log upload file: $name\n\n$data"
}
private companion object { private companion object {
const val TAG = "LogUploadSettings" const val TAG = "LogUploadSettings"
const val UPLOAD_LOGS_KEY = "upload_logs" const val UPLOAD_LOGS_KEY = "upload_logs"