库中的slf4j似乎忽略了我的log4j2配置

ac1kyiln  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(185)

我们在java-ee应用程序中使用Log4j 2。我们使用一个库,其中的日志记录是针对SLF 4J编写的。在这个库中有一个类,它记录了很多我不想要的东西-〉所以我想把这个Logger的LogLevel设置为OFF。
我的log4j2.xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" shutdownHook="disable">
    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT" ignoreExceptions="false" >
            <PatternLayout pattern="%d{ISO8601} %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <JDBC name="DatabaseAppender" tableName="logentry" ignoreExceptions="false" >
            <ConnectionFactory class="xx.xx.xx.xx.LoggingConnectionFactory" method="getDatabaseConnection" />
            <Column name="eventDate" isEventTimestamp="true" />
            <Column name="level" pattern="%level" isUnicode="false"/>
            <Column name="logger" pattern="%logger" isUnicode="false"/>
            <Column name="message" pattern="%message" isUnicode="false"/>
            <Column name="exception" pattern="%throwable{50}" isUnicode="false"/>
        </JDBC>
    </Appenders>
    <Loggers>
        <Logger name="net.rubyeye.xmemcached.transcoders.BaseSerializingTranscoder" level="off" additivity="false">
            <AppenderRef ref="ConsoleAppender"/>
        </Logger>
        <Root level="INFO">
            <AppenderRef ref="DatabaseAppender"/>
            <AppenderRef ref="ConsoleAppender"/>
        </Root>
    </Loggers>
</Configuration>

但是BaseSerializingTranscoder仍然会记录错误。如果我做一个简单的测试,并将BaseSerializingTranscoder中的日志代码放到一个测试函数中,我会发现通过org.slf4j.Logger.LoggerFactory(由库完成)检索到的日志记录器的配置与通过org.apache.logging.log4j.LogManager检索到的日志记录器的配置不一样(我的配置是正确的):

import net.rubyeye.xmemcached.transcoders.BaseSerializingTranscoder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.slf4j.LoggerFactory;

org.slf4j.Logger logger1 = LoggerFactory.getLogger(BaseSerializingTranscoder.class);
logger1.error("log test the same way as in library");

Logger logger2 = LogManager.getLogger(BaseSerializingTranscoder.class);
logger2.error("log test");

logger 1的输出是可见的,这是我想消除的。出了什么问题?

编辑

我们使用Wildfly 10.1。我能够创建一个最小的例子,它有完全相同的问题。
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-all-7.0</artifactId>
            <version>1.0.1.Final</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.9.1</version>
        </dependency>

    </dependencies>

</project>

log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">

    <Appenders>
        <Console name="consoleAppender" target="SYSTEM_OUT">
            <PatternLayout pattern="%d %p %c [%t] %m%n" />
        </Console>
    </Appenders>

    <Loggers>
        <Logger name="blah" level="off" additivity="false">
            <AppenderRef ref="consoleAppender" />
        </Logger>
        <Root level="info">
            <AppenderRef ref="consoleAppender" />
        </Root>
    </Loggers>
</Configuration>

Service.java:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;

@Singleton
@Startup
public class Service {

    private static final Logger loggerBlah = LoggerFactory.getLogger("blah");

    private static final Logger logger = LoggerFactory.getLogger(Service.class);

    @PostConstruct
    private void startup() {
        logger.info("test1");
        logger.error("test2");
        loggerBlah.info("test3");
        loggerBlah.error("test4");
    }
}

输出:

21:13:11,641 INFO  [Service] (ServerService Thread Pool -- 40) test1
21:13:11,641 ERROR [Service] (ServerService Thread Pool -- 40) test2
21:13:11,641 INFO  [blah] (ServerService Thread Pool -- 40) test3
21:13:11,641 ERROR [blah] (ServerService Thread Pool -- 40) test4

不应记录test 3和test 4!出现了什么问题?

epggiuax

epggiuax1#

更新#2:

在您的更新后,我能够复制这个问题。根据我最新的评论,我能够使用this answer来指导我修复这个问题。
我将下面的jboss-deployment-structure.xml添加到Web项目的META-INF中:

<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
    <deployment>
        <exclusions>
            <module name="org.apache.log4j" />
        </exclusions>
        <exclude-subsystems>
            <subsystem name="logging"/>
        </exclude-subsystems>
    </deployment>
</jboss-deployment-structure>

我还稍微修改了log4j2.xml,以便毫无疑问地确定使用了该文件指定的配置。我修改了PatternLayout,添加字符串“MY_PATTERN”作为每个消息的前缀:
<PatternLayout pattern="MY_PATTERN %d %p %c [%t] %m%n" />

输出:

09:30:38,074 INFO  [stdout] (ServerService Thread Pool -- 137) MY_PATTERN 2017-09-24 09:30:38,073 INFO example.Service [ServerService Thread Pool -- 137] test1
09:30:38,077 INFO  [stdout] (ServerService Thread Pool -- 137) MY_PATTERN 2017-09-24 09:30:38,077 ERROR example.Service [ServerService Thread Pool -- 137] test2

消息test 3和test 4不再记录到控制台。

上一个答案和更新:

我怀疑你的问题与你如何配置类路径有关。我强烈建议你检查你的依赖关系层次结构,以确保你有你需要的所有正确的日志库。
下面是将OFF电平与SLF 4J和log4j 2一起使用的简单示例:

package blah;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {

    private static final Logger log = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {

        log.trace("trace log");
        log.debug("debug log");
        log.info("info log");
        log.warn("warn log");
        log.error("error log");
    }

}

以下是log4j2.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">

    <Appenders>
        <Console name="consoleAppender" target="SYSTEM_OUT">
            <PatternLayout pattern="%d %p %c [%t] %m%n" />
        </Console>
    </Appenders>

    <Loggers>
        <Logger name="blah" level="off" additivity="false">
            <AppenderRef ref="consoleAppender" />
        </Logger>
        <Root level="info">
            <AppenderRef ref="consoleAppender" />
        </Root>
    </Loggers>
</Configuration>

以下是相关性:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.9.1</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.9.1</version>
</dependency>

当我把“blah”记录器的级别设置为关闭时,我在控制台上看不到任何日志。当我把它改为trace之类的级别时,我看到了所有的日志。

更新日期:

我能够使用servlet API 3.1.0组装一个简单的Web应用程序,并使用我在上面的示例中提到的依赖项使日志工作(我能够将级别设置为OFF,并且看不到ERROR级别消息)。因此,这肯定看起来是某种配置问题。仅供参考,我将我的简单应用程序部署到Tomcat 8。

相关问题