From ef2d22c95657170ecbd925b5afd9087fa57e4c0e Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 6 Nov 2021 14:12:38 +0000 Subject: [PATCH] Ensure thread safe --- .../src/commonMain/kotlin/internal/utils/StdoutLogger.kt | 5 +++-- .../src/jvmMain/kotlin/utils/PlatformLogger.jvm.kt | 9 +++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/mirai-core-api/src/commonMain/kotlin/internal/utils/StdoutLogger.kt b/mirai-core-api/src/commonMain/kotlin/internal/utils/StdoutLogger.kt index eea798cd5..1b3eb4624 100644 --- a/mirai-core-api/src/commonMain/kotlin/internal/utils/StdoutLogger.kt +++ b/mirai-core-api/src/commonMain/kotlin/internal/utils/StdoutLogger.kt @@ -10,7 +10,6 @@ package net.mamoe.mirai.internal.utils import net.mamoe.mirai.utils.* -import java.text.DateFormat import java.text.SimpleDateFormat import java.util.* @@ -102,7 +101,9 @@ internal open class StdoutLogger constructor( else debug(message.toString()) } - protected open val timeFormat: DateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) + protected open val timeFormat: SimpleDateFormat by threadLocal { + SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) + } private val currentTimeFormatted get() = timeFormat.format(Date()) diff --git a/mirai-core-api/src/jvmMain/kotlin/utils/PlatformLogger.jvm.kt b/mirai-core-api/src/jvmMain/kotlin/utils/PlatformLogger.jvm.kt index 92c149e52..93c8b8743 100644 --- a/mirai-core-api/src/jvmMain/kotlin/utils/PlatformLogger.jvm.kt +++ b/mirai-core-api/src/jvmMain/kotlin/utils/PlatformLogger.jvm.kt @@ -13,6 +13,8 @@ package net.mamoe.mirai.utils import java.text.DateFormat import java.text.SimpleDateFormat +import java.time.LocalDateTime +import java.time.format.DateTimeFormatter import java.util.* /** @@ -106,9 +108,12 @@ public actual open class PlatformLogger constructor( // same as StdoutLogger bu else debug(message.toString()) } - protected open val timeFormat: DateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) + @Deprecated("Use formatter instead.", level = DeprecationLevel.HIDDEN) + protected open val timeFormat: DateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE) - private val currentTimeFormatted get() = timeFormat.format(Date()) + protected open val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") + + private val currentTimeFormatted get() = formatter.format(LocalDateTime.now()) @MiraiExperimentalApi("This is subject to change.") protected enum class Color(private val format: String) {