Skip to content

Commit

Permalink
log files without a "key" (unlimited log file e.g.) does not have a _…
Browse files Browse the repository at this point in the history
… in the file name anymore
  • Loading branch information
Michael Flisar authored and Michael Flisar committed Oct 11, 2024
1 parent ce1b24a commit 5e97f50
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ fun main() {
val setup = FileLoggerSetup.Daily.create(
folder = File(System.getProperty("user.dir"))
)
//val setup2= FileLoggerSetup.SingleFile.create(
// File(System.getProperty("user.dir"), "log.txt")
//)
L.plant(FileLogger(setup))

application {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollState
Expand Down Expand Up @@ -305,7 +304,8 @@ private fun Info(file: Path?, filteredCount: Int, totalCount: Int) {
Row(
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),
) {
val size = (file?.let { FileSystem.SYSTEM.metadataOrNull(it) }?.size?.toDouble() ?: 0.0) / 1000.0
val size =
(file?.let { FileSystem.SYSTEM.metadataOrNull(it) }?.size?.toDouble() ?: 0.0) / 1000.0

val info = ((size * 100).toInt() / 100.0).toString() + "kB"
//val info = "%.2fkB".format((file?.length()?.toDouble() ?: 0.0) / 1000.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import com.michaelflisar.lumberjack.core.CommonIgnoredOnParcel
import com.michaelflisar.lumberjack.implementation.LumberjackLogger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okio.FileSystem
import okio.Path
import okio.Path.Companion.toPath
import okio.SYSTEM

abstract class BaseFileLoggerSetup : FileLoggerSetup() {

Expand All @@ -23,13 +21,18 @@ abstract class BaseFileLoggerSetup : FileLoggerSetup() {

@CommonIgnoredOnParcel
private var lastFileKey: String = ""

@CommonIgnoredOnParcel
private var lastFileKeyChanged: Boolean = false

override fun filePath(data: FileLogger.Event.Data): String {
val lastPath = "${folder}/${fileBaseName}_${lastFileKey}.$fileExtension".toPath()
val key = getFileKey(data, lastPath)
val path = "${folder}/${fileBaseName}_${key}.$fileExtension"
val path = if (key.isEmpty()) {
"${folder}/${fileBaseName}.$fileExtension"
} else {
"${folder}/${fileBaseName}_${key}.$fileExtension"
}
if (key != lastFileKey) {
lastFileKey = key
lastFileKeyChanged = true
Expand All @@ -40,7 +43,7 @@ abstract class BaseFileLoggerSetup : FileLoggerSetup() {
abstract fun getFileKey(data: FileLogger.Event.Data, lastPath: Path): String
abstract fun filterLogFilesToDelete(files: List<Path>): List<Path>

protected fun getKeyFromFile(file: Path) : String {
protected fun getKeyFromFile(file: Path): String {
return file.name.dropLast(fileExtension.length + 1).replace(fileBaseName, "").substring(1)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ fun FileLoggerSetup.SingleFile.Companion.create(
folder.absolutePath,
fileName,
fileExtension
)

fun FileLoggerSetup.SingleFile.Companion.create(
file: File
) = SingleFile(
file.parentFile.absolutePath,
file.nameWithoutExtension,
file.extension
)

0 comments on commit 5e97f50

Please sign in to comment.