mirror of
https://github.com/bbara04/Purefin.git
synced 2026-07-23 19:26:50 +00:00
feat(logging): enhance log entry handling with truncation and improve upload file naming
This commit is contained in:
@@ -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""")
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ class LogUploadSettingsProvider @Inject constructor(
|
||||
}
|
||||
|
||||
logFiles.forEach { logFile ->
|
||||
val uploadedName = jellyfinApiClient.uploadLogFile(logFile.data)
|
||||
val uploadedName = jellyfinApiClient.uploadLogFile(logFile.contentForUpload())
|
||||
?: error("Log upload failed")
|
||||
PurefinLogger.deleteUploadedFile(logFile)
|
||||
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 {
|
||||
const val TAG = "LogUploadSettings"
|
||||
const val UPLOAD_LOGS_KEY = "upload_logs"
|
||||
|
||||
Reference in New Issue
Block a user