如何禁用 mongoDB java 驱动程序日志记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30137564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:12:47  来源:igfitidea点击:

How to disable mongoDB java driver logging?

javamongodblogging

提问by itaied

I am trying to disable log outputs of mongo-java-driver-3.0.0.

我正在尝试禁用mongo-java-driver-3.0.0.

I have tried to set those in the beginning of my application, before loading the mongodrivers, but it didn't help.

在加载mongo驱动程序之前,我尝试在应用程序的开头设置这些,但没有帮助。

    // Enable MongoDB logging in general
    System.setProperty("DEBUG.MONGO", "false");

    // Enable DB operation tracing
    System.setProperty("DB.TRACE", "false");  

I am getting this kind of logs:

我收到这种日志:

11:01:15.406 [pool-1-thread-1] DEBUG org.mongodb.driver.protocol.query - Sending query of namespace susudev.Players on connection [connectionId{localValue:2, serverValue:28}] to server localhost:27017
11:01:15.406 [pool-1-thread-1] DEBUG org.mongodb.driver.protocol.query - Query completed

11:01:25.174 [cluster-ClusterId{value='554dbecb1b554f11e86c3a69', description='null'}-localhost:27017] DEBUG org.mongodb.driver.cluster - Checking status of localhost:27017
11:01:25.177 [cluster-ClusterId{value='554dbecb1b554f11e86c3a69', description='null'}-localhost:27017] DEBUG org.mongodb.driver.cluster - Updating cluster description to  {type=STANDALONE, servers=[{address=localhost:27017, type=STANDALONE, roundTripTime=0.6 ms, state=CONNECTED}]

So my console is completely packed with mongo logs and I cant read anything.

所以我的控制台完全挤满了 mongo 日志,我什么也读不懂。

采纳答案by itaied

So this solved this issue:

所以这解决了这个问题:

import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;

...

static Logger root = (Logger) LoggerFactory
        .getLogger(Logger.ROOT_LOGGER_NAME);

static {
    root.setLevel(Level.INFO);
}

You may set the Levelto a higher Levelif you wish to hide all the logs.

如果您想隐藏所有日志,您可以将 设置Level为更高Level

回答by yuceel

If you need dynamic approach you can iterate over loggers and set level of them. Or you can set levels manually. Here are mongo driver loggers:

如果您需要动态方法,您可以迭代记录器并设置它们的级别。或者您可以手动设置级别。以下是 mongo 驱动程序记录器:

LogManager.getLogger("org.mongodb.driver.connection").setLevel(org.apache.log4j.Level.OFF);
LogManager.getLogger("org.mongodb.driver.management").setLevel(org.apache.log4j.Level.OFF);
LogManager.getLogger("org.mongodb.driver.cluster").setLevel(org.apache.log4j.Level.OFF);
LogManager.getLogger("org.mongodb.driver.protocol.insert").setLevel(org.apache.log4j.Level.OFF);
LogManager.getLogger("org.mongodb.driver.protocol.query").setLevel(org.apache.log4j.Level.OFF);
LogManager.getLogger("org.mongodb.driver.protocol.update").setLevel(org.apache.log4j.Level.OFF);

回答by yoga

use the below import

使用下面的导入

import java.util.logging.Logger;

导入 java.util.logging.Logger;

use the below in code.

在代码中使用以下内容。

Logger mongoLogger = Logger.getLogger( "com.mongodb" ); mongoLogger.setLevel(Level.SEVERE);

Logger mongoLogger = Logger.getLogger("com.mongodb"); mongoLogger.setLevel(Level.SEVERE);

回答by Christophe

For thoses who still face the problem with the logging, you have to set the log level of org.mongodb.driverto something higher like WARN or ERROR. The class com.mongodb is not used anymore even if it is still written in the logs.

对于那些仍然面临日志问题的人,您必须将org.mongodb.driver的日志级别设置为更高的级别,例如 WARN 或 ERROR。com.mongodb 类不再使用,即使它仍然写入日志中。

See the last answer of this post: Configure logging for the MongoDB Java driver

请参阅这篇文章的最后一个答案:为 MongoDB Java 驱动程序配置日志记录

回答by chneau

To make this portion of code working you need to have Logback. (If maven project)

要使这部分代码工作,您需要有Logback。(如果是maven项目)

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.9.0</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
</dependency>

Then if you only want to disable Mongo driver logging, you should do something like this:

然后,如果您只想禁用 Mongo 驱动程序日志记录,您应该执行以下操作:

LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
Logger rootLogger = loggerContext.getLogger("org.mongodb.driver");
rootLogger.setLevel(Level.OFF);

Again to be clear, here is the list of import for this code to work:

再次明确,这里是此代码工作的导入列表:

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import org.slf4j.LoggerFactory;

This solution is for mongo java driver 3.0.0 and ^.

此解决方案适用于 mongo java 驱动程序 3.0.0 和 ^。

Edit: Here is a one liner with level set to ERROR.

编辑:这是一个水平设置为 ERROR 的单班轮。

((LoggerContext) LoggerFactory.getILoggerFactory()).getLogger("org.mongodb.driver").setLevel(Level.ERROR);

回答by Hemant Thorat

I've solved it using ,

我已经解决了它,

import java.util.logging.Logger;
import java.util.logging.Level;

Logger mongoLogger = Logger.getLogger( "org.mongodb.driver" );
mongoLogger.setLevel(Level.SEVERE); // e.g. or Log.WARNING, etc.

回答by Aubin

To turn off all the loggers:

要关闭所有记录器:

final LogManager lm = LogManager.getLogManager();
for( final Enumeration<String> i = lm.getLoggerNames(); i.hasMoreElements(); ) {
   lm.getLogger( i.nextElement()).setLevel( Level.OFF );
}

回答by Mohammad Rafigh

in case you use xml resource to configure Logback you can easily do it by adding this:

如果您使用 xml 资源配置 Logback,您可以通过添加以下内容轻松完成:

<logger name="org.mongodb.driver.cluster" level="OFF" />

<logger name="org.mongodb.driver.cluster" level="OFF" />

to your configuration.

到您的配置。

回答by Giorgio Vespucci

In my case, MongoDB driver 3.5.0, SLF4j+Log4j12, it's been sufficient adding:

就我而言,MongoDB driver 3.5.0, SLF4j+Log4j12,添加以下内容就足够了:

log4j.logger.org.mongodb.driver=OFF

in my Log4j .propertiesconfiguration file.

在我的 Log4j.properties配置文件中。

回答by derfect

import your Mongo client through "com.mongodb.client.MongoClient"

通过“com.mongodb。导入蒙戈客户.MongoClient”

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;

import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Quick
{

    public static void main(String[] args)
    {
        Logger.getLogger("org.mongodb.driver").setLevel(Level.WARNING);
        try (MongoClient mongo = MongoClients.create())
        {
            mongo.listDatabaseNames().forEach((Consumer<String>) System.out::println);
        }
    }
}

make sure you have the latest version of the driver, 3.12.2at the time I wrote this answer

确保在我写这个答案时你有最新版本的驱动程序,3.12.2

<dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.12.2</version>
</dependency>


if the above doesn't work, you're probably using a different logging module, look up how to turn that off, for example if you're using slf4j, create a file named "simpleLogger.properties" inside your resources folder and add this line to it

如果上述方法不起作用,则您可能使用了不同的日志记录模块,请查看如何将其关闭,例如,如果您使用的是slf4j,请在您的资源文件夹中创建一个名为“ simpleLogger.properties”的文件并添加这条线到它

org.slf4j.simpleLogger.defaultLogLevel = warn