我们在java-ee应用程序中使用Log4j2。我们使用一个库,根据SLF4J对日志进行编程。在这个库中是一个类,它记录了很多我不想要的东西 - >所以我想将此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");
logger1的输出是可见的,我想消除它。有什么问题?
修改
我们使用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
不应记录test3和test4! 有什么问题?
答案 0 :(得分:3)
更新#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
消息test3和test4不再记录到控制台。
以前的答案和更新:
我怀疑您的问题与您如何配置类路径有关。我强烈建议您检查依赖关系层次结构,以确保拥有所需的所有正确的日志记录库。
以下是使用SLF4J和log4j2的OFF级别的简单示例:
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”记录器的级别设置为off时,我看不到控制台上的任何日志。当我将其更改为跟踪时,我会看到所有日志。
<强>更新强>
我能够使用servlet API 3.1.0组建一个简单的Web应用程序,并使用我在上面的示例中提到的依赖项使日志工作(我能够将级别设置为OFF并且不能看到ERROR级别消息)。所以它肯定似乎是某种配置问题。就像仅供参考我将我的简单应用程序部署到Tomcat 8一样。