Improve logging in components

- Fix duplicated event logs
- Specify logger name for AccountSecretsManager
- Fix OtherClientUpdater logger
- Unify ServerList logger
- SubLogger.isEnabled
This commit is contained in:
Him188 2021-04-29 16:29:43 +08:00
parent 060004644e
commit 1bb1b18f98
6 changed files with 29 additions and 14 deletions

View File

@ -134,7 +134,7 @@ internal open class QQAndroidBot constructor(
BdhSessionSyncer,
BdhSessionSyncerImpl(configuration, components, networkLogger.subLogger("BotSessionSyncer"))
)
set(ServerList, ServerListImpl())
set(ServerList, ServerListImpl(networkLogger.subLogger("ServerList")))
set(PacketLoggingStrategy, PacketLoggingStrategyImpl(bot))
set(
PacketHandler, PacketHandlerChain(
@ -146,7 +146,7 @@ internal open class QQAndroidBot constructor(
set(PacketCodec, PacketCodecImpl())
set(
OtherClientUpdater,
OtherClientUpdaterImpl(bot, components, networkLogger.subLogger("BotSessionSyncer"))
OtherClientUpdaterImpl(bot, components, networkLogger.subLogger("OtherClientUpdater"))
)
set(ConfigPushSyncer, ConfigPushSyncerImpl())

View File

@ -9,7 +9,6 @@
package net.mamoe.mirai.internal.network.components
import net.mamoe.mirai.event.Event
import net.mamoe.mirai.event.events.MessageEvent
import net.mamoe.mirai.internal.AbstractBot
import net.mamoe.mirai.internal.contact.logMessageReceived
@ -64,14 +63,14 @@ internal class PacketLoggingStrategyImpl(
}
private fun logReceivedImpl(packet: Packet, incomingPacket: IncomingPacket, logger: MiraiLogger) {
when {
packet is MessageEvent -> packet.logMessageReceived()
packet is Packet.NoLog -> {
when (packet) {
is MessageEvent -> packet.logMessageReceived()
is Packet.NoLog -> {
// nothing to do
}
packet is Event && packet !is Packet.NoEventLog -> bot.logger.verbose {
"Event: $packet".replaceMagicCodes()
}
// packet is Event && packet !is Packet.NoEventLog -> bot.logger.verbose {
// "Event: $packet".replaceMagicCodes()
// } // processed in global `Event.broadcast`
else -> {
if (SHOW_PACKET_DETAILS) {
logger.verbose { "Recv: ${incomingPacket.commandName} ${incomingPacket.data}".replaceMagicCodes() }

View File

@ -12,6 +12,9 @@ package net.mamoe.mirai.internal.network.components
import kotlinx.serialization.Serializable
import net.mamoe.mirai.internal.network.component.ComponentKey
import net.mamoe.mirai.internal.network.components.ServerList.Companion.DEFAULT_SERVER_LIST
import net.mamoe.mirai.utils.MiraiLogger
import net.mamoe.mirai.utils.info
import org.jetbrains.annotations.TestOnly
import java.net.InetSocketAddress
import java.util.*
@ -25,6 +28,10 @@ internal data class ServerAddress(
require(host.isNotBlank()) { "host is invalid: '$host'" }
}
override fun toString(): String {
return "$host:$port"
}
fun toSocketAddress(): InetSocketAddress = InetSocketAddress.createUnresolved(host, port)
}
@ -78,8 +85,15 @@ internal interface ServerList {
}
internal class ServerListImpl(
private val logger: MiraiLogger,
initial: Collection<ServerAddress> = emptyList()
) : ServerList {
@TestOnly
constructor(initial: Collection<ServerAddress>) : this(MiraiLogger.TopLevel, initial)
@TestOnly
constructor() : this(MiraiLogger.TopLevel)
@Volatile
private var preferred: Set<ServerAddress> = DEFAULT_SERVER_LIST
@ -88,6 +102,7 @@ internal class ServerListImpl(
@Synchronized
override fun setPreferred(list: Collection<ServerAddress>) {
logger.info { "Server list: ${list.joinToString()}." }
require(list.isNotEmpty()) { "list cannot be empty." }
preferred = list.toSet()
}

View File

@ -15,6 +15,7 @@ import net.mamoe.mirai.internal.network.component.ComponentKey
import net.mamoe.mirai.internal.network.components.AccountSecretsManager
import net.mamoe.mirai.internal.network.components.SsoProcessor
import net.mamoe.mirai.internal.network.components.createAccountsSecretsManager
import net.mamoe.mirai.internal.utils.subLogger
import net.mamoe.mirai.utils.BotConfiguration
import net.mamoe.mirai.utils.DeviceInfo
@ -45,6 +46,9 @@ internal class SsoProcessorContextImpl(
override val account: BotAccount get() = bot.account
override val device: DeviceInfo = configuration.deviceInfo?.invoke(bot) ?: DeviceInfo.random()
override val protocol: BotConfiguration.MiraiProtocol get() = configuration.protocol
override val accountSecretsManager: AccountSecretsManager get() = configuration.createAccountsSecretsManager(bot.logger)
override val accountSecretsManager: AccountSecretsManager
get() = configuration.createAccountsSecretsManager(
bot.logger.subLogger("AccountSecretsManager")
)
override val configuration: BotConfiguration get() = bot.configuration
}

View File

@ -23,7 +23,6 @@ import net.mamoe.mirai.internal.network.components.BdhSessionSyncer
import net.mamoe.mirai.internal.network.components.ServerAddress
import net.mamoe.mirai.internal.network.components.ServerList
import net.mamoe.mirai.internal.network.context.BdhSession
import net.mamoe.mirai.internal.network.handler.logger
import net.mamoe.mirai.internal.network.networkType
import net.mamoe.mirai.internal.network.protocol.data.jce.FileStoragePushFSSvcList
import net.mamoe.mirai.internal.network.protocol.data.jce.PushResp
@ -139,7 +138,6 @@ internal class ConfigPushSvc {
}
fun handleServerListPush(resp: PushReqResponse.ServerListPush) {
bot.network.logger.info { "Server list updated." }
val serverListPush = kotlin.runCatching {
resp.struct.jcebuf.loadAs(ServerListPush.serializer())
}.getOrElse {
@ -154,8 +152,6 @@ internal class ConfigPushSvc {
serverListPush.mobileSSOServerList
}
bot.network.logger.info { "Server list: ${pushServerList.joinToString()}." }
if (pushServerList.isNotEmpty()) {
bot.components[ServerList].setPreferred(
pushServerList.shuffled().map { ServerAddress(it.host, it.port) })

View File

@ -20,6 +20,7 @@ private class SubLogger(
private val main: MiraiLogger,
) : MiraiLoggerPlatformBase() {
override val identity: String? get() = main.identity
override val isEnabled: Boolean get() = main.isEnabled
override fun verbose0(message: String?, e: Throwable?) {
if (message != null) {