-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Fix cannot read property isPrefixForAll from YAML #21
🔥 Remove output not by mirai logger ⚡ Use launch for logger close #21
- Loading branch information
1 parent
1ee392a
commit 8a2ad46
Showing
4 changed files
with
48 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,89 @@ | ||
package com.pigeonyuze | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.* | ||
import net.mamoe.mirai.message.data.ForwardMessageBuilder | ||
import net.mamoe.mirai.message.data.PlainText | ||
import java.text.SimpleDateFormat | ||
import java.util.* | ||
import java.time.LocalDateTime | ||
import java.time.format.DateTimeFormatter | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
@Suppress("UNUSED") | ||
object LoggerManager { | ||
object LoggerManager : CoroutineScope { | ||
|
||
private inline val notRun get() = !LoggerConfig.open | ||
override val coroutineContext: CoroutineContext = CoroutineExceptionHandler { _, error -> | ||
loggingError(error) | ||
} + YamlBot.coroutineContext + SupervisorJob(YamlBot.coroutineContext[Job]) | ||
|
||
private val miraiLogger = YamlBot.logger | ||
private val miraiLogger by lazy { YamlBot.logger } | ||
|
||
private const val defaultfrom = "YamlBot" | ||
|
||
private val dateFormat: DateTimeFormatter = | ||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") | ||
|
||
@JvmOverloads | ||
@JvmStatic | ||
fun loggingError(from: Any = defaultfrom, message: String?) { | ||
if (notRun) return | ||
if (!LoggerConfig.open) return | ||
miraiLogger.error("[$from] $message") | ||
trySendLogMessage(message ?: "") | ||
trySendLogMessage("ERROR", from.toString(), message ?: "") | ||
} | ||
|
||
fun loggingError(error: Throwable) { | ||
if (!LoggerConfig.open) return | ||
miraiLogger.error(error) | ||
trySendLogMessage("ERROR", error.javaClass.simpleName, error.message ?: "") | ||
} | ||
|
||
@JvmOverloads | ||
@JvmStatic | ||
fun loggingWarn(from: Any = defaultfrom, message: String?) { | ||
if (notRun) return | ||
if (!LoggerConfig.open) return | ||
miraiLogger.warning("[$from] $message") | ||
} | ||
|
||
@JvmOverloads | ||
@JvmStatic | ||
fun loggingInfo(from: Any = defaultfrom, message: String?) { | ||
if (notRun) return | ||
if (!LoggerConfig.open) return | ||
miraiLogger.info("[$from] $message") | ||
} | ||
|
||
@JvmOverloads | ||
@JvmStatic | ||
fun loggingDebug(from: Any = defaultfrom, message: String?) { | ||
if (notRun) return | ||
if (!LoggerConfig.open) return | ||
miraiLogger.debug("[$from] $message") | ||
} | ||
|
||
@JvmOverloads | ||
@JvmStatic | ||
fun loggingTrace(from: Any = defaultfrom, message: String?) { | ||
if (notRun) return | ||
if (!LoggerConfig.open) return | ||
miraiLogger.verbose("[$from] $message") | ||
} | ||
|
||
private fun trySendLogMessage(message: String) { | ||
val bot = BotsTool.firstBot | ||
val group = BotsTool.getGroupOrNullJava(LoggerConfig.debugGroup) ?: return | ||
val forwardMessageBuilder = ForwardMessageBuilder(group) | ||
forwardMessageBuilder.add( | ||
bot, | ||
PlainText("${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())} log message\nlevel type = ERROR") | ||
) | ||
forwardMessageBuilder.add(bot, PlainText(message)) | ||
runBlocking { | ||
@Suppress("SameParameterValue") | ||
private fun trySendLogMessage(level: String, from: String, message: String) { | ||
if (LoggerConfig.debugGroup == 0L) return | ||
launch { | ||
val group = BotsTool.getGroupOrNull(LoggerConfig.debugGroup) ?: return@launch | ||
|
||
val forwardMessageBuilder = ForwardMessageBuilder(group) | ||
.add( | ||
group.bot, | ||
PlainText("${LocalDateTime.now().format(dateFormat)} log message") | ||
) | ||
.add( | ||
group.bot, | ||
PlainText("level type = $level\nfrom = $from\nmessage = $message") | ||
) | ||
|
||
group.sendMessage( | ||
forwardMessageBuilder.build() | ||
.copy(title = "Log信息", summary = "level = ERROR", preview = listOf("(●'◡'●)")) | ||
.copy(title = "Log信息", summary = "level = $level", preview = listOf("(●'◡'●)")) | ||
) | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters