Skip to content

Instantly share code, notes, and snippets.

@Siltal
Created October 11, 2022 13:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Siltal/1e4c0d57d9bc2e2d048057b4e376f03c to your computer and use it in GitHub Desktop.
Save Siltal/1e4c0d57d9bc2e2d048057b4e376f03c to your computer and use it in GitHub Desktop.
如何隐藏 Mirai 的 Log4j 警告

在使用 mirai 的无头客户端时,总是会出现如下警告,这通常是由于没有提供日志的实现导致的。

2022-10-11 11:45:14 W/stderr: ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/log4j/2.x/manual/configuration.html for instructions on how to configure Log4j 2

如果你只是需要去除红色提示(强迫症),可以参考如下过程

  • 进入 mirai 根目录
cd /path/to/mirai-console-loader/
  • 下载依赖
wget https://repo1.maven.org/maven2/org/slf4j/slf4j-nop/1.7.36/slf4j-nop-1.7.36.jar -P libs
wget https://repo1.maven.org/maven2/org/apache/logging/log4j/log4j-core/2.12.1/log4j-core-2.12.1.jar -P libs
  • 配置下载桥接/接口
cat > plugin-shared-libraries/libraries.txt << EOF
org.slf4j:slf4j-api:1.7.36
net.mamoe:mirai-slf4j-bridge:1.2.0
EOF
  • 编辑 log4j2 配置文件
cat > config/log4j2.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="error">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>
EOF
  • 写入启动脚本(use Console)
cat > runConsole << EOF
java -cp "./libs/*" -Dlog4j2.configurationFile=config/log4j2.xml net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader \$*
EOF

chmod +x runConsole
  • 写入启动脚本(use mcl)
cat > mcl << EOF
#!/usr/bin/env sh
export JAVA_BINARY=java
#\$JAVA_BINARY -jar mcl.jar $*
\$JAVA_BINARY -cp libs/*:mcl.jar -Dlog4j2.configurationFile=config/log4j2.xml org.itxtech.mcl.Loader \$*
EOF

感谢 @StageGurad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment