Ensure thread safe

This commit is contained in:
Him188 2021-11-06 14:12:38 +00:00
parent 4a488f3416
commit ef2d22c956
2 changed files with 10 additions and 4 deletions

View File

@ -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())

View File

@ -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) {