如何使用log4j抑制来自外部库的日志信息?[英] How do I supress log messages from external libraries, using log4j

本文是小编为大家收集整理的关于如何使用log4j抑制来自外部库的日志信息?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用 log4j2 为项目进行一些记录.我的配置中的登录部分看起来像这样.

<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 supress supers

提供了任何效果.

我搜索了" io.netty.util记录",并提出了 this .看起来有一种覆盖默认记录器

方法
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"的方法.

所以,我对如何进行此操作有些困惑. Log4J有没有办法以我想的方式调试外部库来调试/信息级消息?

推荐答案

如果记录器是在log4j 1.x API中定义的(答案的原始版本):

问题是log4j 1.x api不使用log4j2属性.

添加log4j-1.2-api.jar,作为log4j 1.x桥,它应该解决您的问题.

,但似乎Netty的默认LoggerFactory是Slf4JloggerFactory,因此实际上需要Waht是Slf4J Bridges. slf4j-log4j12至log4j 1.x,log4j-slf4j-impl to log4j2.x.

本文地址:https://www.itbaoku.cn/post/1575079.html

问题描述

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>

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;
}

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".

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?

推荐答案

If the logger is defined in log4j 1.x api ( original version of the answer):

The problem is that log4j 1.x api don't use log4j2 property.

Add log4j-1.2-api.jar, as a log4j 1.x bridge, it should fix your problem.

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.