diff --git a/src/main/kotlin/cn/org/subit/SSubitO.kt b/src/main/kotlin/cn/org/subit/SSubitO.kt index 0607503..10639dd 100644 --- a/src/main/kotlin/cn/org/subit/SSubitO.kt +++ b/src/main/kotlin/cn/org/subit/SSubitO.kt @@ -90,7 +90,6 @@ fun main(args: Array) // 创建一个临时文件, 用于存储合并后的配置文件 val tempFile = File.createTempFile("resConfig", ".yaml") tempFile.writeText(Yaml.encodeToString(resConfig)) - println(tempFile.readText()) val resArgs = args1 + "-config=${tempFile.absolutePath}" diff --git a/src/main/kotlin/cn/org/subit/config/LoggerConfig.kt b/src/main/kotlin/cn/org/subit/config/LoggerConfig.kt index dd614d6..a93367e 100644 --- a/src/main/kotlin/cn/org/subit/config/LoggerConfig.kt +++ b/src/main/kotlin/cn/org/subit/config/LoggerConfig.kt @@ -4,6 +4,7 @@ import cn.org.subit.console.ColorDisplayMode import cn.org.subit.console.Console import cn.org.subit.console.EffectDisplayMode import cn.org.subit.logger.SSubitOLogger +import cn.org.subit.logger.ToFileHandler import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlinx.serialization.Transient @@ -51,5 +52,6 @@ var loggerConfig: LoggerConfig by config( if (new.effect) EffectDisplayMode.ON else EffectDisplayMode.OFF Console.ansiColorMode = new.color + ToFileHandler.clearOld(new.logFileSaveTime) } ) \ No newline at end of file diff --git a/src/main/kotlin/cn/org/subit/logger/SSubitOLogger.kt b/src/main/kotlin/cn/org/subit/logger/SSubitOLogger.kt index 99b1539..f1b4313 100644 --- a/src/main/kotlin/cn/org/subit/logger/SSubitOLogger.kt +++ b/src/main/kotlin/cn/org/subit/logger/SSubitOLogger.kt @@ -27,6 +27,7 @@ import java.util.zip.ZipOutputStream import kotlin.jvm.optionals.getOrDefault import kotlin.reflect.KClass import kotlin.reflect.jvm.jvmName +import kotlin.time.Duration /** * logger系统 @@ -309,7 +310,6 @@ object ToFileHandler: Handler() } logFile.createNewFile() // 创建新的log文件 cnt = 0 // 重置行数 - clearOld() // 清理过期log } /** @@ -332,21 +332,28 @@ object ToFileHandler: Handler() fos.close() } - private fun clearOld() + fun clearOld(duration: Duration = loggerConfig.logFileSaveTime) { val files = logDir.listFiles() ?: return files.asSequence() .filter { it.name.endsWith(".zip") } - .map { it to fileDateFormat.parse(it.name.substringBeforeLast(".zip")) } + .map { it to it.name.substringBeforeLast(".zip") } + .map { runCatching { it.first to fileDateFormat.parse(it.second) }.getOrNull() } + .filterNotNull() .map { it.first to it.second.toInstant().toKotlinInstant() } - .map { it.first to it.second - Clock.System.now() } - .filter { it.second > loggerConfig.logFileSaveTime } - .forEach { it.first.delete() } + .map { it.first to (Clock.System.now() - it.second) } + .filter { it.second > duration } + .map { it.first } + .forEach { it.delete() } } private fun check() { - if ((cnt ushr 10) > 0) new() + if ((cnt ushr 10) > 0) + { + new() + clearOld() + } } private fun append(lines: List) = synchronized(this)