mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-25 15:40:28 +08:00
Review LoggerController
- Delete logger cache - Move MiraiConsoleLogger to internal package
This commit is contained in:
parent
cef1326432
commit
9f88293691
@ -38,10 +38,8 @@ import net.mamoe.mirai.console.internal.extension.GlobalComponentStorage
|
|||||||
import net.mamoe.mirai.console.internal.permission.BuiltInPermissionService
|
import net.mamoe.mirai.console.internal.permission.BuiltInPermissionService
|
||||||
import net.mamoe.mirai.console.internal.plugin.PluginManagerImpl
|
import net.mamoe.mirai.console.internal.plugin.PluginManagerImpl
|
||||||
import net.mamoe.mirai.console.internal.util.autoHexToBytes
|
import net.mamoe.mirai.console.internal.util.autoHexToBytes
|
||||||
import net.mamoe.mirai.console.logging.LoggerController
|
import net.mamoe.mirai.console.logging.*
|
||||||
import net.mamoe.mirai.console.logging.LoggerControllerForFrontend
|
import net.mamoe.mirai.console.internal.logging.MiraiConsoleLogger
|
||||||
import net.mamoe.mirai.console.logging.MiraiConsoleLoggerUnused
|
|
||||||
import net.mamoe.mirai.console.logging.MiraiDelegateLogger
|
|
||||||
import net.mamoe.mirai.console.permission.PermissionService
|
import net.mamoe.mirai.console.permission.PermissionService
|
||||||
import net.mamoe.mirai.console.permission.PermissionService.Companion.grantPermission
|
import net.mamoe.mirai.console.permission.PermissionService.Companion.grantPermission
|
||||||
import net.mamoe.mirai.console.permission.RootPermission
|
import net.mamoe.mirai.console.permission.RootPermission
|
||||||
@ -99,10 +97,7 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI
|
|||||||
|
|
||||||
override fun createLogger(identity: String?): MiraiLogger {
|
override fun createLogger(identity: String?): MiraiLogger {
|
||||||
val controller = loggerController
|
val controller = loggerController
|
||||||
val logger = MiraiConsoleLoggerUnused(controller, identity)
|
return MiraiConsoleLogger(controller, controller.newLogger(identity))
|
||||||
val delegate = MiraiDelegateLogger(logger)
|
|
||||||
logger.delegateLogger = delegate
|
|
||||||
return delegate
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("RemoveRedundantBackticks")
|
@Suppress("RemoveRedundantBackticks")
|
||||||
|
@ -11,12 +11,20 @@
|
|||||||
package net.mamoe.mirai.console.internal.data.builtins
|
package net.mamoe.mirai.console.internal.data.builtins
|
||||||
|
|
||||||
import net.mamoe.mirai.console.data.AutoSavePluginConfig
|
import net.mamoe.mirai.console.data.AutoSavePluginConfig
|
||||||
|
import net.mamoe.mirai.console.data.ValueDescription
|
||||||
import net.mamoe.mirai.console.data.value
|
import net.mamoe.mirai.console.data.value
|
||||||
import net.mamoe.mirai.utils.SimpleLogger
|
import net.mamoe.mirai.utils.SimpleLogger
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
internal object LoggerConfig : AutoSavePluginConfig("Logger") {
|
internal object LoggerConfig : AutoSavePluginConfig("Logger") {
|
||||||
|
@ValueDescription("""
|
||||||
|
日志输出等级 可选值: ALL, VERBOSE, DEBUG, INFO, WARNING, ERROR, NONE
|
||||||
|
""")
|
||||||
val defaultPriority by value(LogPriority.INFO)
|
val defaultPriority by value(LogPriority.INFO)
|
||||||
|
|
||||||
|
@ValueDescription("""
|
||||||
|
特定日志记录器输出等级
|
||||||
|
""")
|
||||||
val loggers: Map<String, LogPriority> by value(
|
val loggers: Map<String, LogPriority> by value(
|
||||||
mapOf("example.logger" to LogPriority.NONE)
|
mapOf("example.logger" to LogPriority.NONE)
|
||||||
)
|
)
|
||||||
|
@ -8,8 +8,9 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package net.mamoe.mirai.console.logging
|
package net.mamoe.mirai.console.internal.logging
|
||||||
|
|
||||||
|
import net.mamoe.mirai.console.logging.LoggerController
|
||||||
import net.mamoe.mirai.utils.MiraiLogger
|
import net.mamoe.mirai.utils.MiraiLogger
|
||||||
import net.mamoe.mirai.utils.MiraiLoggerPlatformBase
|
import net.mamoe.mirai.utils.MiraiLoggerPlatformBase
|
||||||
import net.mamoe.mirai.utils.SimpleLogger
|
import net.mamoe.mirai.utils.SimpleLogger
|
@ -28,8 +28,6 @@ import java.util.concurrent.atomic.AtomicReference
|
|||||||
public interface LoggerController {
|
public interface LoggerController {
|
||||||
/** 是否应该记录该等级的日志 */
|
/** 是否应该记录该等级的日志 */
|
||||||
public fun shouldLog(identity: String?, priority: SimpleLogger.LogPriority): Boolean
|
public fun shouldLog(identity: String?, priority: SimpleLogger.LogPriority): Boolean
|
||||||
/** 是否应该对 [newLogger] 的结果进行缓存 */
|
|
||||||
public val cacheLoggers: Boolean
|
|
||||||
/**
|
/**
|
||||||
* 创建一个新的 [MiraiLogger]
|
* 创建一个新的 [MiraiLogger]
|
||||||
*
|
*
|
||||||
@ -43,13 +41,4 @@ public interface LoggerController {
|
|||||||
* 因此不要在 [newLogger] 中调用 [net.mamoe.mirai.utils.DefaultLogger]
|
* 因此不要在 [newLogger] 中调用 [net.mamoe.mirai.utils.DefaultLogger]
|
||||||
*/
|
*/
|
||||||
public fun newLogger(identity: String?): MiraiLogger
|
public fun newLogger(identity: String?): MiraiLogger
|
||||||
/**
|
|
||||||
* 获取对应的 Registration
|
|
||||||
*
|
|
||||||
* 实现细节:
|
|
||||||
* - 如果 [identity] 存在对应 Registration, 直接返回先前的 Registration
|
|
||||||
* - [identity] 不存在对应 Registration时, 创建新的 Registration 并存储起来
|
|
||||||
* - 注意并发性
|
|
||||||
*/
|
|
||||||
public fun getLoggerRegistration(identity: String?): AtomicReference<Any>
|
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ package net.mamoe.mirai.console.logging
|
|||||||
|
|
||||||
import net.mamoe.mirai.console.ConsoleFrontEndImplementation
|
import net.mamoe.mirai.console.ConsoleFrontEndImplementation
|
||||||
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
|
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
|
||||||
|
import net.mamoe.mirai.utils.MiraiLogger
|
||||||
import net.mamoe.mirai.utils.SimpleLogger
|
import net.mamoe.mirai.utils.SimpleLogger
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import java.util.concurrent.atomic.AtomicReference
|
import java.util.concurrent.atomic.AtomicReference
|
||||||
@ -22,12 +23,5 @@ import java.util.concurrent.atomic.AtomicReference
|
|||||||
public abstract class LoggerControllerPlatformBase : LoggerController {
|
public abstract class LoggerControllerPlatformBase : LoggerController {
|
||||||
|
|
||||||
override fun shouldLog(identity: String?, priority: SimpleLogger.LogPriority): Boolean = true
|
override fun shouldLog(identity: String?, priority: SimpleLogger.LogPriority): Boolean = true
|
||||||
override val cacheLoggers: Boolean get() = true
|
|
||||||
protected val registrations: ConcurrentHashMap<Any, AtomicReference<Any>> = ConcurrentHashMap()
|
|
||||||
|
|
||||||
protected object NilIdentityPlaceholder
|
|
||||||
|
|
||||||
override fun getLoggerRegistration(identity: String?): AtomicReference<Any> =
|
|
||||||
registrations.computeIfAbsent(identity ?: NilIdentityPlaceholder) { AtomicReference() }
|
|
||||||
|
|
||||||
}
|
}
|
@ -1,56 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 via the following link.
|
|
||||||
*
|
|
||||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
package net.mamoe.mirai.console.logging
|
|
||||||
|
|
||||||
import net.mamoe.mirai.utils.MiraiLogger
|
|
||||||
import net.mamoe.mirai.utils.MiraiLoggerPlatformBase
|
|
||||||
|
|
||||||
internal class MiraiConsoleLoggerUnused(
|
|
||||||
val controller: LoggerController,
|
|
||||||
override val identity: String?
|
|
||||||
) : MiraiLoggerPlatformBase() {
|
|
||||||
internal object InitializeLock
|
|
||||||
|
|
||||||
internal lateinit var delegateLogger: MiraiDelegateLogger
|
|
||||||
private val logger: MiraiLogger by lazy {
|
|
||||||
val logger = if (controller.cacheLoggers) {
|
|
||||||
val reference = controller.getLoggerRegistration(identity)
|
|
||||||
if (reference.compareAndSet(null, InitializeLock)) {
|
|
||||||
val logger = MiraiConsoleLogger(controller, controller.newLogger(identity))
|
|
||||||
reference.set(logger)
|
|
||||||
logger
|
|
||||||
} else {
|
|
||||||
// initialized/initializing
|
|
||||||
val logger: MiraiLogger
|
|
||||||
while (true) {
|
|
||||||
val status = reference.get()
|
|
||||||
if (status is MiraiLogger) {
|
|
||||||
logger = status
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
logger
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
MiraiConsoleLogger(controller, controller.newLogger(identity))
|
|
||||||
}
|
|
||||||
delegateLogger.logger = logger
|
|
||||||
logger
|
|
||||||
}
|
|
||||||
override val isEnabled: Boolean get() = logger.isEnabled
|
|
||||||
|
|
||||||
override fun debug0(message: String?, e: Throwable?) = logger.debug(message, e)
|
|
||||||
override fun error0(message: String?, e: Throwable?) = logger.error(message, e)
|
|
||||||
override fun info0(message: String?, e: Throwable?) = logger.info(message, e)
|
|
||||||
override fun verbose0(message: String?, e: Throwable?) = logger.verbose(message, e)
|
|
||||||
override fun warning0(message: String?, e: Throwable?) = logger.warning(message, e)
|
|
||||||
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 via the following link.
|
|
||||||
*
|
|
||||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
package net.mamoe.mirai.console.logging
|
|
||||||
|
|
||||||
import net.mamoe.mirai.utils.MiraiLogger
|
|
||||||
import net.mamoe.mirai.utils.MiraiLoggerPlatformBase
|
|
||||||
|
|
||||||
internal class MiraiDelegateLogger(var logger: MiraiLogger) : MiraiLoggerPlatformBase() {
|
|
||||||
override val identity: String? get() = logger.identity
|
|
||||||
override val isEnabled: Boolean get() = logger.isEnabled
|
|
||||||
override fun debug0(message: String?, e: Throwable?) = logger.debug(message, e)
|
|
||||||
override fun error0(message: String?, e: Throwable?) = logger.error(message, e)
|
|
||||||
override fun info0(message: String?, e: Throwable?) = logger.info(message, e)
|
|
||||||
override fun verbose0(message: String?, e: Throwable?) = logger.verbose(message, e)
|
|
||||||
override fun warning0(message: String?, e: Throwable?) = logger.warning(message, e)
|
|
||||||
}
|
|
@ -72,7 +72,7 @@ fun initTestEnvironment() {
|
|||||||
override fun createLoginSolver(requesterBot: Long, configuration: BotConfiguration): LoginSolver =
|
override fun createLoginSolver(requesterBot: Long, configuration: BotConfiguration): LoginSolver =
|
||||||
LoginSolver.Default
|
LoginSolver.Default
|
||||||
|
|
||||||
override val loggerController: LoggerController = object:LoggerControllerPlatformBase(){
|
override val loggerController: LoggerController = object : LoggerControllerPlatformBase() {
|
||||||
override fun shouldLog(identity: String?, priority: SimpleLogger.LogPriority): Boolean = true
|
override fun shouldLog(identity: String?, priority: SimpleLogger.LogPriority): Boolean = true
|
||||||
override fun newLogger(identity: String?): MiraiLogger = PlatformLogger(identity)
|
override fun newLogger(identity: String?): MiraiLogger = PlatformLogger(identity)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user