如何在java中使自定义Mirai的日志系统?
-
回复: 如何完全使用其他日志系统
- 我创建了com.config.MiraiLoggerFactory实现了net.mamoe.mirai.utils.MiraiLogger.Factory
public class MiraiLoggerFactory implements MiraiLogger.Factory { @NotNull @Override public MiraiLogger create(@NotNull Class<?> aClass, @Nullable String s) { return (MiraiLogger) LoggerFactory.getLogger(aClass); } }
- 我在META-INF/services创建了文件net.mamoe.mirai.utils.MiraiLogger.Factory
com.config.MiraiLoggerFactory
但不知到为什么,Mirai-Core还是没有使用我的日志配置
-
-
-
@cssxsh @Nambers
翻看源码发现:BotConfiguration
提供了方法setNetworkLoggerSupplier
和setBotLoggerSupplier
。然后我实现了MiraiLogger接口,然后定义了LoggerFactory内部类
import net.mamoe.mirai.utils.MiraiLogger; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; public class SpringMiraiLogger implements MiraiLogger { private final Logger logger; private final String identity; private boolean enabled = true; public SpringMiraiLogger(Class<?> clazz, String identity) { logger = org.slf4j.LoggerFactory.getLogger(clazz); this.identity = clazz.getSimpleName(); } public SpringMiraiLogger(Class<?> clazz, String identity, boolean enabled) { logger = org.slf4j.LoggerFactory.getLogger(clazz); this.identity = clazz.getSimpleName(); this.enabled = enabled; } public static class LoggerFactory implements MiraiLogger.Factory { public static final MiraiLogger.Factory INSTANCE = new LoggerFactory(); @NotNull @Override public MiraiLogger create(@NotNull Class<?> clazz) { return new SpringMiraiLogger(clazz, clazz.getSimpleName()); } @NotNull @Override public MiraiLogger create(@NotNull Class<?> clazz, @Nullable String identity) { return new SpringMiraiLogger(clazz, identity); } } @Nullable @Override public String getIdentity() { return identity; } public boolean setEnabled(boolean enabled) { return this.enabled = enabled; } @Override public boolean isEnabled() { return enabled; } @Override public void debug(@Nullable String s) { logger.debug(s); } @Override public void debug(@Nullable String s, @Nullable Throwable throwable) { logger.debug(s, throwable); } @Override public void error(@Nullable String s) { logger.error(s); } @Override public void error(@Nullable String s, @Nullable Throwable throwable) { logger.error(s, throwable); } @Override public void info(@Nullable String s) { logger.info(s); } @Override public void info(@Nullable String s, @Nullable Throwable throwable) { logger.info(s, throwable); } @Override public void verbose(@Nullable String s) { logger.trace(s); } @Override public void verbose(@Nullable String s, @Nullable Throwable throwable) { logger.trace(s, throwable); } @Override public void warning(@Nullable String s) { logger.warn(s); } @Override public void warning(@Nullable String s, @Nullable Throwable throwable) { logger.warn(s, throwable); } }
我在BotConfiguration中调用了
setNetworkLoggerSupplier
和setBotLoggerSupplier
。this.setBotLoggerSupplier(bot -> SpringMiraiLogger.LoggerFactory.INSTANCE.create(bot.getClass())); this.setNetworkLoggerSupplier(bot -> SpringMiraiLogger.LoggerFactory.INSTANCE.create(bot.getClass()));
成功实现日志转接!
-
你没必要自己手动写我给的链接都说明了
你引用这个包就好
net.mamoe:mirai-logging-slf4j
-
@cssxsh 引入那个包会报错,不知道为什么
-
@2446694 mirai-logging-log4j2
-
-
@2446694 不过这个报错好像没啥影响 Failed to load class "org.slf4j.impl.StaticLoggerBinder".
-
@cssxsh 在 如何在java中使自定义Mirai的日志系统? 中说:
这两个依赖同时加上就没问题了<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>net.mamoe</groupId> <artifactId>mirai-logging-log4j2</artifactId> <version>${mirai.version}</version> <scope>runtime</scope> </dependency>
-
-
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>net.mamoe</groupId> <artifactId>mirai-logging-slf4j</artifactId> <version>${mirai.version}</version> <scope>runtime</scope> </dependency>
Exception in thread "main" java.lang.ExceptionInInitializerError at com.MiraiApplication.main(MiraiApplication.java:11) Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j at org.apache.logging.slf4j.Log4jLoggerFactory.validateContext(Log4jLoggerFactory.java:60) at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:44) at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:33) at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:53) at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:33) at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:363) at org.apache.commons.logging.LogAdapter$Slf4jAdapter.createLocationAwareLog(LogAdapter.java:130) at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:91) at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67) at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:59) at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:178) ... 1 more
-
@2446694 在 如何在java中使自定义Mirai的日志系统? 中说:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> <scope>runtime</scope> </dependency>
spring-boot-starter-log4j2 和 mirai-logging-slf4j
冲突我的意思是 spring-boot 的默认 日志系统是 slf4j
所以你 直接使用 mirai-logging-slf4j 就好 -
@cssxsh 哦,我还以为加上mirai-logging-slf4j以后,就可以引入任意的日志系统(/捂脸)