diff --git a/backend/mirai-console/src/MiraiConsole.kt b/backend/mirai-console/src/MiraiConsole.kt index fd4a21560..8360bd388 100644 --- a/backend/mirai-console/src/MiraiConsole.kt +++ b/backend/mirai-console/src/MiraiConsole.kt @@ -30,6 +30,7 @@ import net.mamoe.mirai.console.util.ConsoleInternalApi import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScopeContext import net.mamoe.mirai.console.util.SemVersion import net.mamoe.mirai.utils.BotConfiguration +import net.mamoe.mirai.utils.DefaultLogger import net.mamoe.mirai.utils.MiraiLogger import java.io.File import java.nio.file.Path @@ -140,6 +141,9 @@ public interface MiraiConsole : CoroutineScope { var config = BotConfiguration().apply { fileBasedDeviceInfo() redirectNetworkLogToDirectory() + this.botLoggerSupplier = { + DefaultLogger("Bot.${it.id}") + } parentCoroutineContext = MiraiConsole.childScopeContext("Bot $id") this.loginSolver = MiraiConsoleImplementationBridge.createLoginSolver(id, this) diff --git a/backend/mirai-console/src/internal/data/builtins/LoggerConfig.kt b/backend/mirai-console/src/internal/data/builtins/LoggerConfig.kt index 49375d866..447051689 100644 --- a/backend/mirai-console/src/internal/data/builtins/LoggerConfig.kt +++ b/backend/mirai-console/src/internal/data/builtins/LoggerConfig.kt @@ -27,6 +27,7 @@ internal object LoggerConfig : AutoSavePluginConfig("Logger") { mapOf( "example.logger" to AbstractLoggerController.LogPriority.NONE, "console.debug" to AbstractLoggerController.LogPriority.NONE, + "Bot" to AbstractLoggerController.LogPriority.ALL, ) ) diff --git a/backend/mirai-console/src/internal/logging/LoggerControllerImpl.kt b/backend/mirai-console/src/internal/logging/LoggerControllerImpl.kt index 836583e7e..2f7273d79 100644 --- a/backend/mirai-console/src/internal/logging/LoggerControllerImpl.kt +++ b/backend/mirai-console/src/internal/logging/LoggerControllerImpl.kt @@ -16,15 +16,18 @@ import net.mamoe.mirai.console.util.ConsoleInternalApi @ConsoleFrontEndImplementation @ConsoleInternalApi -internal object LoggerControllerImpl : AbstractLoggerController() { +internal object LoggerControllerImpl : AbstractLoggerController.PathBased() { internal var initialized = false - override fun getPriority(identity: String?): LogPriority { + override fun findPriority(identity: String?): LogPriority? { if (!initialized) return LogPriority.NONE return if (identity == null) { LoggerConfig.defaultPriority } else { - LoggerConfig.loggers[identity] ?: LoggerConfig.defaultPriority + LoggerConfig.loggers[identity] } } + + override val defaultPriority: LogPriority + get() = if (initialized) LoggerConfig.defaultPriority else LogPriority.NONE } \ No newline at end of file diff --git a/backend/mirai-console/src/logging/AbstractLoggerController.kt b/backend/mirai-console/src/logging/AbstractLoggerController.kt index a3105bcac..c9a58e965 100644 --- a/backend/mirai-console/src/logging/AbstractLoggerController.kt +++ b/backend/mirai-console/src/logging/AbstractLoggerController.kt @@ -13,19 +13,33 @@ import net.mamoe.mirai.console.util.ConsoleExperimentalApi import net.mamoe.mirai.utils.SimpleLogger import java.util.* +/** + * 日志控制器的基本实现 + */ @ConsoleExperimentalApi public abstract class AbstractLoggerController : LoggerController { + /** + * @param priority 尝试判断的日志等级 + * @param settings 配置中的日志等级 (see [getPriority]) + */ protected open fun shouldLog( priority: LogPriority, settings: LogPriority, ): Boolean = settings <= priority + /** + * 获取配置中与 [identity] 对应的 [LogPriority] + */ protected abstract fun getPriority(identity: String?): LogPriority override fun shouldLog(identity: String?, priority: SimpleLogger.LogPriority): Boolean = shouldLog(LogPriority.by(priority), getPriority(identity)) + /** + * 便于进行配置存储的 [LogPriority], + * 等级优先级与 [SimpleLogger.LogPriority] 对应 + */ @Suppress("unused") @ConsoleExperimentalApi public enum class LogPriority { @@ -59,4 +73,64 @@ public abstract class AbstractLoggerController : LoggerController { } + /** + * 路径形式实现的基本日志控制器 + * + * Example: + * 配置文件: + * ``` + * defaultPriority: ALL + * loggers: + * t: NONE + * t.sub: VERBOSE + * t.sub.1: NONE + * ``` + * + * ``` + * "logger.1" + * -> "logger.1" << null + * -> "logger" << null + * -> defaultPriority << ALL + * + * "t.sub.1" + * -> "t.sub.1" << NONE + * + * "t.sub.2" + * -> "t.sub.2" << null + * -> "t.sub" << VERBOSE + * + * ...... + * ``` + */ + @ConsoleExperimentalApi + public abstract class PathBased + @JvmOverloads public constructor( + protected open val spliterator: Char = '.' + ) : AbstractLoggerController() { + protected abstract val defaultPriority: LogPriority + protected abstract fun findPriority(identity: String?): LogPriority? + + /** + * 从 [path] 析出下一次应该进行搜索的二次 path (@see [getPriority]) + * + * @return 如果返回了 `null`, 会令 [getPriority] 返回 `findPriority(null) ?: defaultPriority`) + */ + protected open fun nextPath(path: String): String? { + val lastIndex = path.lastIndexOf(spliterator) + if (lastIndex == -1) return null + return path.substring(0, lastIndex) + } + + override fun getPriority(identity: String?): LogPriority { + if (identity == null) { + return findPriority(null) ?: defaultPriority + } else { + var path: String = identity + while (true) { + findPriority(path)?.let { return it } + path = nextPath(path) ?: return (findPriority(null) ?: defaultPriority) + } + } + } + } } diff --git a/backend/mirai-console/src/logging/LoggerController.kt b/backend/mirai-console/src/logging/LoggerController.kt index 59b60457b..cdb96a203 100644 --- a/backend/mirai-console/src/logging/LoggerController.kt +++ b/backend/mirai-console/src/logging/LoggerController.kt @@ -9,14 +9,17 @@ package net.mamoe.mirai.console.logging -import net.mamoe.mirai.console.internal.logging.LoggerControllerImpl +import net.mamoe.mirai.console.MiraiConsoleImplementation +import net.mamoe.mirai.console.internal.logging.MiraiConsoleLogger import net.mamoe.mirai.console.util.ConsoleExperimentalApi import net.mamoe.mirai.utils.SimpleLogger /** * 日志控制系统 * - * @see [LoggerControllerImpl] + * @see [AbstractLoggerController] + * @see [MiraiConsoleImplementation.loggerController] + * @see [MiraiConsoleLogger] */ @ConsoleExperimentalApi public interface LoggerController { diff --git a/backend/mirai-console/test/logging/TestALC_PathBased.kt b/backend/mirai-console/test/logging/TestALC_PathBased.kt new file mode 100644 index 000000000..72c45beb2 --- /dev/null +++ b/backend/mirai-console/test/logging/TestALC_PathBased.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2019-2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.console.logging + +import org.junit.jupiter.api.Test + +@Suppress("ClassName") +internal class TestALC_PathBased { + @Test + fun `test AbstractLoggerController$PathBased`() { + val config = mapOf( + "test" to "ALL", + "test.test" to "VERBOSE", + "test.test.test" to "NONE", + ).mapValues { AbstractLoggerController.LogPriority.valueOf(it.value) } + + val c = object : AbstractLoggerController.PathBased() { + override val defaultPriority: LogPriority + get() = LogPriority.NONE + + override fun findPriority(identity: String?): LogPriority? { + if (identity == null) return defaultPriority + return config[identity] + } + + fun priority(i: String?): LogPriority = getPriority(i) + } + + fun assertSame(path: String?, p: String) { + kotlin.test.assertSame(c.priority(path), AbstractLoggerController.LogPriority.valueOf(p)) + } + + assertSame("test.test.test", "NONE") + assertSame("test.test.test.more.test", "NONE") + + assertSame("test.test.t1", "VERBOSE") + assertSame("test.test.t15w", "VERBOSE") + assertSame("test.test", "VERBOSE") + + assertSame("test", "ALL") + assertSame("test.tes1ww", "ALL") + assertSame("test.asldjawe.awej2oi3", "ALL") + + assertSame("AWawex", "NONE") + assertSame("awpejaszx.aljewkz", "NONE") + assertSame("test0.awekjo23xxxxx", "NONE") + } +}