Skip to content

Commit

Permalink
fix: 修复logger相关错误
Browse files Browse the repository at this point in the history
  • Loading branch information
nullaqua committed Sep 17, 2024
1 parent 39d7ad0 commit fb09002
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/main/kotlin/cn/org/subit/SSubitO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ fun main(args: Array<String>)
// 创建一个临时文件, 用于存储合并后的配置文件
val tempFile = File.createTempFile("resConfig", ".yaml")
tempFile.writeText(Yaml.encodeToString(resConfig))
println(tempFile.readText())

val resArgs = args1 + "-config=${tempFile.absolutePath}"

Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/cn/org/subit/config/LoggerConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
)
21 changes: 14 additions & 7 deletions src/main/kotlin/cn/org/subit/logger/SSubitOLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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系统
Expand Down Expand Up @@ -309,7 +310,6 @@ object ToFileHandler: Handler()
}
logFile.createNewFile() // 创建新的log文件
cnt = 0 // 重置行数
clearOld() // 清理过期log
}

/**
Expand All @@ -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<String>) = synchronized(this)
Expand Down

0 comments on commit fb09002

Please sign in to comment.