Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

插件模式 在 mirai 2.12.0 下失效 #4

Closed
cssxsh opened this issue Jul 4, 2022 · 1 comment
Closed

插件模式 在 mirai 2.12.0 下失效 #4

cssxsh opened this issue Jul 4, 2022 · 1 comment

Comments

@cssxsh
Copy link
Contributor

cssxsh commented Jul 4, 2022

原因可能是类隔离机制导致 对 LoggerFactory 的修改失效

kotlin.runCatching {
Class.forName("android.view.View")
// Unsafe 100% 无法在 Android 平台工作
logger.error("Mirai Logger Binding Slf4j with Console Plugin mode NOT SUPPORTED on Android")
}.onFailure {
// 如果不使用 Unsafe, 将无法修改 static final 字段
val unsafe = Unsafe.getUnsafe()
val LF = LoggerFactory::class.java
unsafe.ensureClassInitialized(LF)
val field = LF.getDeclaredField("NOP_FALLBACK_FACTORY")
val LFBase = unsafe.staticFieldBase(field)
val LFOffset = unsafe.staticFieldOffset(field)
unsafe.putReference(LFBase, LFOffset, MiraiLoggerSlf4jFactoryNOP)
val INITIALIZATION_STATE = LF.getDeclaredField("INITIALIZATION_STATE")
val NOP_FALLBACK_INITIALIZATION = LF.getDeclaredField("NOP_FALLBACK_INITIALIZATION")
Root.openAccess(INITIALIZATION_STATE)
Root.openAccess(NOP_FALLBACK_INITIALIZATION)
INITIALIZATION_STATE.setInt(null, NOP_FALLBACK_INITIALIZATION.getInt(null))
logger.debug("SLF4J LOGGER FACTORY = " + LoggerFactory.getILoggerFactory())
}
}

@cssxsh
Copy link
Contributor Author

cssxsh commented Jul 4, 2022

目前的解决方案是

plugin-shared-libraries/libraries.txt 中加入

org.slf4j:slf4j-api:1.7.36
net.mamoe:mirai-slf4j-bridge:1.2.0

@cssxsh cssxsh closed this as not planned Won't fix, can't repro, duplicate, stale Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant