1+ package me.arasple.mc.trchat.module.internal.logger
2+
3+ import org.slf4j.LoggerFactory
4+ import taboolib.common.LifeCycle
5+ import taboolib.common.platform.Awake
6+ import taboolib.common.platform.Platform
7+ import taboolib.common.platform.PlatformSide
8+ import taboolib.common.platform.function.submit
9+ import java.io.PrintStream
10+
11+ /* *
12+ * https://github.com/8aka-Team/Invero/blob/main/src/main/kotlin/cc/trixey/invero/common/logger/SLF4JLoggerSuppressor.kt
13+ * SLF4J日志抑制器
14+ * 用于屏蔽SLF4J的初始化警告日志
15+ */
16+ @PlatformSide(Platform .BUKKIT )
17+ object SLF4JLoggerSuppressor {
18+
19+ @Awake(LifeCycle .ENABLE )
20+ fun setupLoggerSuppressor () {
21+ // 保存原始的System.err输出流
22+ val originalErr = System .err
23+
24+ submit {
25+ // 恢复原始的错误输出流
26+ System .setErr(originalErr)
27+ }
28+
29+ try {
30+ // 设置一个临时的错误输出流,过滤掉SLF4J相关的初始化警告
31+ System .setErr(object : PrintStream (originalErr) {
32+ override fun println (message : String? ) {
33+ if (! isSLF4JInitMessage(message)) {
34+ super .println (message)
35+ }
36+ }
37+
38+ override fun print (message : String? ) {
39+ if (! isSLF4JInitMessage(message)) {
40+ super .print (message)
41+ }
42+ }
43+ })
44+
45+ // 触发SLF4J的初始化
46+ LoggerFactory .getLogger(SLF4JLoggerSuppressor ::class .java)
47+
48+ } catch (_: Exception ) {
49+ }
50+ }
51+
52+ /* *
53+ * 判断是否为SLF4J的初始化消息
54+ */
55+ private fun isSLF4JInitMessage (message : String? ): Boolean {
56+ if (message == null ) return false
57+ return message.contains(" SLF4J:" ) && (
58+ message.contains(" No SLF4J providers were found" ) ||
59+ message.contains(" Defaulting to no-operation (NOP) logger" ) ||
60+ message.contains(" See https://www.slf4j.org/codes.html" ) ||
61+ message.contains(" StaticLoggerBinder" ) ||
62+ message.contains(" slf4j-api" ) ||
63+ message.contains(" slf4j-simple" ) ||
64+ message.contains(" SLF4J initialization" )
65+ )
66+ }
67+ }
0 commit comments