停止在 java 中打印 slf4j 消息
Stop slf4j messages from printing in java
我有下面的代码,不希望这些日志消息被打印出来printed.I也想停止打印来自同一项目中其他库的日志消息。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main{
public static void main(String[] args) {
final Logger slf4jLogger = LoggerFactory.getLogger(Main.class);
slf4jLogger.info("im printing info");
slf4jLogger.error("printing error");
}
我在 src/test/resources 下也添加了 log4j.properties 到类路径中。
log4j.properties如下
log4j.rootLogger=OFF
log4j.logger.main=OFF
我想阻止这些日志打印到控制台。
但它不工作并打印日志。
这是我遗漏的东西吗?
我假设你使用的是 Log4J 2,根据 documentation 要求,在没有 log4j.configurationFile
系统 属性 的情况下,配置文件被命名为 log4j2.properties
,而不是像你那样 log4j.properties
。
正如文档所说:
If no configuration file could be located the DefaultConfiguration
will be used. This will cause logging output to go to the console.
您在 /src/test/resources
而不是 /src/main/resources
中获得配置文件也很奇怪,因此根据您认为如何将配置文件放到类路径中,这也可能导致一个问题。
我有下面的代码,不希望这些日志消息被打印出来printed.I也想停止打印来自同一项目中其他库的日志消息。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main{
public static void main(String[] args) {
final Logger slf4jLogger = LoggerFactory.getLogger(Main.class);
slf4jLogger.info("im printing info");
slf4jLogger.error("printing error");
}
我在 src/test/resources 下也添加了 log4j.properties 到类路径中。 log4j.properties如下
log4j.rootLogger=OFF
log4j.logger.main=OFF
我想阻止这些日志打印到控制台。 但它不工作并打印日志。 这是我遗漏的东西吗?
我假设你使用的是 Log4J 2,根据 documentation 要求,在没有 log4j.configurationFile
系统 属性 的情况下,配置文件被命名为 log4j2.properties
,而不是像你那样 log4j.properties
。
正如文档所说:
If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.
您在 /src/test/resources
而不是 /src/main/resources
中获得配置文件也很奇怪,因此根据您认为如何将配置文件放到类路径中,这也可能导致一个问题。