如何使用log4j禁止来自外部库的日志消息 [英] How do I supress log messages from external libraries, using log4j
问题描述
我正在使用 log4j2 为项目做一些日志记录.配置中的我的记录器"部分如下所示.
I am using log4j2 to do some logging for a project. My Loggers section in my config looks like this.
<Loggers>
<!-- default logger -->
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
<!-- only log warn/error/fatal for external libs -->
<Logger name="org.cfg4j" level="warn" additivity="false">
<AppenderRef ref="Console"></AppenderRef>
</Logger>
<Logger name="io.netty.util" level="warn" additivity="false">
<AppenderRef ref="Console"></AppenderRef>
</Logger>
</Loggers>
这似乎对抑制io.netty.util中的DEBUG级别的消息没有任何作用
This doesn't seem to be having any effect as far as supressing the DEBUG level messages from io.netty.util
I googled "io.netty.util logging" and came up with this. It looks like there is a way to override the default logger
public abstract class InternalLoggerFactory {
private static volatile InternalLoggerFactory defaultFactory;
static {
final String name = InternalLoggerFactory.class.getName();
InternalLoggerFactory f;
try {
f = new Slf4JLoggerFactory(true);
f.newInstance(name).debug("Using SLF4J as the default logging framework");
defaultFactory = f;
} catch (Throwable t1) {
try {
f = new Log4JLoggerFactory();
f.newInstance(name).debug("Using Log4J as the default logging framework");
} catch (Throwable t2) {
f = new JdkLoggerFactory();
f.newInstance(name).debug("Using java.util.logging as the default logging framework");
}
}
defaultFactory = f;
}
我尝试了一下,但是遇到了一些问题.首先,我的IDE(intellij)告诉我不赞成使用Log4JLoggerFactory.我继续,但随后发现该实例不包含名为"newInstance"的方法.
I tried giving this a go, butI ran into a few issues. For one, my IDE (intellij) told me that Log4JLoggerFactory is deprecated. I proceeded, but then found out that the instance did not contain a method called "newInstance".
因此,我对如何进行此操作感到有些困惑.有没有办法让log4j按照我的想法抑制外部库DEBUG/INFO级别的消息?
So, I'm a little confused on how to proceed with this. Is there a way for log4j to supress external libraries DEBUG/INFO level messages in the way that I am thinking?
推荐答案
如果记录器是在log4j 1.x api(答案的原始版本)中定义的:
If the logger is defined in log4j 1.x api ( original version of the answer):
问题是log4j 1.x api不使用log4j2属性.
The problem is that log4j 1.x api don't use log4j2 property.
添加log4j-1.2-api.jar作为log4j 1.x桥,它将解决您的问题.
Add log4j-1.2-api.jar, as a log4j 1.x bridge, it should fix your problem.
但是净值的默认loggerfactory是Slf4JLoggerFactory,所以实际需要的是slf4j桥. slf4j-log4j12到log4j 1.x,log4j-slf4j-impl到log4j2.x.
But seems default loggerfactory of netty is Slf4JLoggerFactory, so waht actually needed are the slf4j bridges. slf4j-log4j12 to log4j 1.x, log4j-slf4j-impl to log4j2.x.
这篇关于如何使用log4j禁止来自外部库的日志消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!