This commit is contained in:
Horis
2024-07-30 13:02:45 +08:00
parent 452925f419
commit beb561a052
3 changed files with 20 additions and 28 deletions

View File

@@ -0,0 +1,18 @@
package io.legado.app.utils
import io.legado.app.help.globalExecutor
import java.util.logging.FileHandler
import java.util.logging.LogRecord
class AsyncFileHandler(pattern: String) : FileHandler(pattern) {
override fun publish(record: LogRecord?) {
if (!isLoggable(record)) {
return
}
globalExecutor.execute {
super.publish(record)
}
}
}

View File

@@ -1,25 +0,0 @@
package io.legado.app.utils
import io.legado.app.help.globalExecutor
import java.util.logging.Handler
import java.util.logging.LogRecord
class AsyncHandler(private val delegate: Handler) : Handler() {
override fun publish(record: LogRecord?) {
globalExecutor.execute {
delegate.publish(record)
}
}
override fun flush() {
delegate.flush()
}
override fun close() {
delegate.close()
}
}
fun Handler.asynchronous() = AsyncHandler(this)

View File

@@ -11,7 +11,6 @@ import io.legado.app.help.config.AppConfig
import splitties.init.appCtx
import java.text.SimpleDateFormat
import java.util.Date
import java.util.logging.FileHandler
import java.util.logging.Level
import java.util.logging.LogRecord
import java.util.logging.Logger
@@ -58,7 +57,7 @@ object LogUtils {
}
val date = getCurrentDateStr(TIME_PATTERN)
val logPath = FileUtils.getPath(root = logFolder, "appLog-$date.txt")
FileHandler(logPath).apply {
AsyncFileHandler(logPath).apply {
formatter = object : java.util.logging.Formatter() {
override fun format(record: LogRecord): String {
// 设置文件输出格式
@@ -70,7 +69,7 @@ object LogUtils {
} else {
Level.OFF
}
}.asynchronous()
}
}
fun upLevel() {