From d54f4cd0d909d9f669a06b271dc4226e12684924 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Mon, 9 Nov 2020 13:33:05 +0800 Subject: [PATCH] console.debug logger --- .../mirai-console/src/data/AutoSavePluginData.kt | 2 +- .../internal/MiraiConsoleImplementationBridge.kt | 1 + .../src/internal/command/CommandManagerImpl.kt | 6 +++++- .../src/internal/data/builtins/LoggerConfig.kt | 5 ++++- .../src/internal/logging/LoggerControllerImpl.kt | 13 ++++++++----- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/backend/mirai-console/src/data/AutoSavePluginData.kt b/backend/mirai-console/src/data/AutoSavePluginData.kt index ff281c02f..36a1071c9 100644 --- a/backend/mirai-console/src/data/AutoSavePluginData.kt +++ b/backend/mirai-console/src/data/AutoSavePluginData.kt @@ -146,7 +146,7 @@ public open class AutoSavePluginData private constructor( } internal val debuggingLogger1 by lazy { - DefaultLogger("debug").withSwitch(false) + DefaultLogger("console.debug").withSwitch(false) } @Suppress("RESULT_CLASS_IN_RETURN_TYPE") diff --git a/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt b/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt index 11e148b50..b86024a88 100644 --- a/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt +++ b/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt @@ -109,6 +109,7 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI if (loggerController === LoggerControllerImpl) { // Reload LoggerConfig. ConsoleDataScope.addAndReloadConfig(LoggerConfig) + LoggerControllerImpl.initialized = true } } diff --git a/backend/mirai-console/src/internal/command/CommandManagerImpl.kt b/backend/mirai-console/src/internal/command/CommandManagerImpl.kt index 9b6f3ecd5..8640664f1 100644 --- a/backend/mirai-console/src/internal/command/CommandManagerImpl.kt +++ b/backend/mirai-console/src/internal/command/CommandManagerImpl.kt @@ -20,6 +20,7 @@ import net.mamoe.mirai.console.command.CommandSender.Companion.toCommandSender import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors import net.mamoe.mirai.console.command.parse.CommandCallParser.Companion.parseCommandCall import net.mamoe.mirai.console.command.resolve.CommandCallResolver.Companion.resolve +import net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge import net.mamoe.mirai.console.permission.PermissionService.Companion.testPermission import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope import net.mamoe.mirai.event.Listener @@ -30,6 +31,7 @@ import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.asMessageChain import net.mamoe.mirai.message.data.content import net.mamoe.mirai.utils.MiraiLogger +import net.mamoe.mirai.utils.SimpleLogger import java.util.concurrent.locks.ReentrantLock @OptIn(ExperimentalCommandDescriptors::class) @@ -75,7 +77,9 @@ internal object CommandManagerImpl : CommandManager, CoroutineScope by MiraiCons when (val result = executeCommand(sender, message)) { is CommandExecuteResult.PermissionDenied -> { if (!result.command.prefixOptional || message.content.startsWith(CommandManager.commandPrefix)) { - sender.sendMessage("权限不足") + if (MiraiConsoleImplementationBridge.loggerController.shouldLog("console.debug", SimpleLogger.LogPriority.DEBUG)) { + sender.sendMessage("权限不足") + } intercept() } } diff --git a/backend/mirai-console/src/internal/data/builtins/LoggerConfig.kt b/backend/mirai-console/src/internal/data/builtins/LoggerConfig.kt index f860e104a..49375d866 100644 --- a/backend/mirai-console/src/internal/data/builtins/LoggerConfig.kt +++ b/backend/mirai-console/src/internal/data/builtins/LoggerConfig.kt @@ -24,7 +24,10 @@ internal object LoggerConfig : AutoSavePluginConfig("Logger") { 特定日志记录器输出等级 """) val loggers: Map by value( - mapOf("example.logger" to AbstractLoggerController.LogPriority.NONE) + mapOf( + "example.logger" to AbstractLoggerController.LogPriority.NONE, + "console.debug" to AbstractLoggerController.LogPriority.NONE, + ) ) } diff --git a/backend/mirai-console/src/internal/logging/LoggerControllerImpl.kt b/backend/mirai-console/src/internal/logging/LoggerControllerImpl.kt index c65a23237..836583e7e 100644 --- a/backend/mirai-console/src/internal/logging/LoggerControllerImpl.kt +++ b/backend/mirai-console/src/internal/logging/LoggerControllerImpl.kt @@ -17,11 +17,14 @@ import net.mamoe.mirai.console.util.ConsoleInternalApi @ConsoleFrontEndImplementation @ConsoleInternalApi internal object LoggerControllerImpl : AbstractLoggerController() { + internal var initialized = false - override fun getPriority(identity: String?): LogPriority = if (identity == null) { - LoggerConfig.defaultPriority - } else { - LoggerConfig.loggers[identity] ?: LoggerConfig.defaultPriority + override fun getPriority(identity: String?): LogPriority { + if (!initialized) return LogPriority.NONE + return if (identity == null) { + LoggerConfig.defaultPriority + } else { + LoggerConfig.loggers[identity] ?: LoggerConfig.defaultPriority + } } - } \ No newline at end of file