简体   繁体   中英

mongodb driver - how to hide debug logging with log4j?

Mongo java driver is verbose since v3.xx, it's not very optimal to store a log of logs like this. Would like to hide some of them.

Here are my dependencies (maven / pom.xml)

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

 <!-- LOGS -->
  <dependency>
     <groupId>org.slf4j</groupId>
     <artifactId>slf4j-api</artifactId>
     <version>1.7.5</version>
  </dependency>
  <dependency>
     <groupId>log4j</groupId>
     <artifactId>log4j</artifactId>
     <version>1.2.17</version>
  </dependency>    
  <dependency>
     <groupId>org.slf4j</groupId>
     <artifactId>jcl-over-slf4j</artifactId>
     <version>1.7.5</version>
  </dependency>
  <dependency>
     <groupId>org.slf4j</groupId>
     <artifactId>jul-to-slf4j</artifactId>
     <version>1.7.5</version>
  </dependency>
  <dependency>
     <groupId>org.slf4j</groupId>
     <artifactId>slf4j-log4j12</artifactId>
     <version>1.7.5</version>
  </dependency>

I want to hide mongo java driver logs :

2015-05-16 10:43:22 023 DEBUG command:56 - Sending command {count : BsonString{value='skydatas'}} to database rizze on connection [connectionId{localValue:2, serverValue:9}] to server 127.0.0.1:27017
2015-05-16 10:43:22 041 DEBUG command:56 - Command execution completed
2015-05-16 10:43:22 042 DEBUG command:56 - Sending command {count : BsonString{value='skydatas'}} to database rizze on connection [connectionId{localValue:2, serverValue:9}] to server 127.0.0.1:27017
2015-05-16 10:43:22 060 DEBUG command:56 - Command execution completed


2015-05-16T10:49:34.448Z DEBUG Checking status of 127.0.0.1:27017 |   | cluster:56
2015-05-16T10:49:34.452Z DEBUG Updating cluster description to  {type=STANDALONE, servers=[{address=127.0.0.1:27017, type=STANDALONE, roundTripTime=1,3 ms, state=CONNECTED}] |   | cluster:56

Thanks

You should be able to do this from your log4j configuration file.

The first thing I would do is temporarily add the fully-qualified class name to your log4j appender format ( %C if you're using a formatter based on PatternLayout ). That gives you the package and class names of the source of the logging statements you want to exclude.

Then follow the example from the log4j manual to specifically raise the logging level of the packages/classes you don't want to see (search the page for text from the snippet below to find it on the page for more context):

# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN

Don't forget to take the %C out of your pattern when you've excluded all the packages you don't want to see.

Here is a log4j configuration, I'm using with log4j:

# Log4J logger file
log = ./log4j
log4j.rootLogger = WARN, CONSOLE
log4j.logger.com.rizze.db=INFO


# Direct log messages to stdout
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss SSS} %-5p %m / %c{1}:%L %n

What is doing this work around - show Logs with level >= INFO for package com.rizze.db - show others Logs with level >= WARN

it is work for me.

add this line to you log4j configuration to fix this problem.

log4j.logger.org.mongodb.driver=ERROR

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM