PathBased LoggerController; KDoc

close #228
This commit is contained in:
Karlatemp 2020-11-28 13:25:30 +08:00
parent 0bc810e42c
commit e391936679
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
6 changed files with 145 additions and 5 deletions

View File

@ -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.CoroutineScopeUtils.childScopeContext
import net.mamoe.mirai.console.util.SemVersion import net.mamoe.mirai.console.util.SemVersion
import net.mamoe.mirai.utils.BotConfiguration import net.mamoe.mirai.utils.BotConfiguration
import net.mamoe.mirai.utils.DefaultLogger
import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.mirai.utils.MiraiLogger
import java.io.File import java.io.File
import java.nio.file.Path import java.nio.file.Path
@ -140,6 +141,9 @@ public interface MiraiConsole : CoroutineScope {
var config = BotConfiguration().apply { var config = BotConfiguration().apply {
fileBasedDeviceInfo() fileBasedDeviceInfo()
redirectNetworkLogToDirectory() redirectNetworkLogToDirectory()
this.botLoggerSupplier = {
DefaultLogger("Bot.${it.id}")
}
parentCoroutineContext = MiraiConsole.childScopeContext("Bot $id") parentCoroutineContext = MiraiConsole.childScopeContext("Bot $id")
this.loginSolver = MiraiConsoleImplementationBridge.createLoginSolver(id, this) this.loginSolver = MiraiConsoleImplementationBridge.createLoginSolver(id, this)

View File

@ -27,6 +27,7 @@ internal object LoggerConfig : AutoSavePluginConfig("Logger") {
mapOf( mapOf(
"example.logger" to AbstractLoggerController.LogPriority.NONE, "example.logger" to AbstractLoggerController.LogPriority.NONE,
"console.debug" to AbstractLoggerController.LogPriority.NONE, "console.debug" to AbstractLoggerController.LogPriority.NONE,
"Bot" to AbstractLoggerController.LogPriority.ALL,
) )
) )

View File

@ -16,15 +16,18 @@ import net.mamoe.mirai.console.util.ConsoleInternalApi
@ConsoleFrontEndImplementation @ConsoleFrontEndImplementation
@ConsoleInternalApi @ConsoleInternalApi
internal object LoggerControllerImpl : AbstractLoggerController() { internal object LoggerControllerImpl : AbstractLoggerController.PathBased() {
internal var initialized = false internal var initialized = false
override fun getPriority(identity: String?): LogPriority { override fun findPriority(identity: String?): LogPriority? {
if (!initialized) return LogPriority.NONE if (!initialized) return LogPriority.NONE
return if (identity == null) { return if (identity == null) {
LoggerConfig.defaultPriority LoggerConfig.defaultPriority
} else { } else {
LoggerConfig.loggers[identity] ?: LoggerConfig.defaultPriority LoggerConfig.loggers[identity]
} }
} }
override val defaultPriority: LogPriority
get() = if (initialized) LoggerConfig.defaultPriority else LogPriority.NONE
} }

View File

@ -13,19 +13,33 @@ import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.utils.SimpleLogger import net.mamoe.mirai.utils.SimpleLogger
import java.util.* import java.util.*
/**
* 日志控制器的基本实现
*/
@ConsoleExperimentalApi @ConsoleExperimentalApi
public abstract class AbstractLoggerController : LoggerController { public abstract class AbstractLoggerController : LoggerController {
/**
* @param priority 尝试判断的日志等级
* @param settings 配置中的日志等级 (see [getPriority])
*/
protected open fun shouldLog( protected open fun shouldLog(
priority: LogPriority, priority: LogPriority,
settings: LogPriority, settings: LogPriority,
): Boolean = settings <= priority ): Boolean = settings <= priority
/**
* 获取配置中与 [identity] 对应的 [LogPriority]
*/
protected abstract fun getPriority(identity: String?): LogPriority protected abstract fun getPriority(identity: String?): LogPriority
override fun shouldLog(identity: String?, priority: SimpleLogger.LogPriority): Boolean = override fun shouldLog(identity: String?, priority: SimpleLogger.LogPriority): Boolean =
shouldLog(LogPriority.by(priority), getPriority(identity)) shouldLog(LogPriority.by(priority), getPriority(identity))
/**
* 便于进行配置存储的 [LogPriority],
* 等级优先级与 [SimpleLogger.LogPriority] 对应
*/
@Suppress("unused") @Suppress("unused")
@ConsoleExperimentalApi @ConsoleExperimentalApi
public enum class LogPriority { 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)
}
}
}
}
} }

View File

@ -9,14 +9,17 @@
package net.mamoe.mirai.console.logging 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.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.utils.SimpleLogger import net.mamoe.mirai.utils.SimpleLogger
/** /**
* 日志控制系统 * 日志控制系统
* *
* @see [LoggerControllerImpl] * @see [AbstractLoggerController]
* @see [MiraiConsoleImplementation.loggerController]
* @see [MiraiConsoleLogger]
*/ */
@ConsoleExperimentalApi @ConsoleExperimentalApi
public interface LoggerController { public interface LoggerController {

View File

@ -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")
}
}