mongodb driver - how to hide debug logging with log4j? - java

Mongo java driver is verbose since v3.x.x, 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

Related

java mail aws smtp: javax.mail.AuthenticationFailedException: 220 Ready to start TLS

I have configured the below properties for smtp appender. the same configuration works fine in local but getting error on AWS
log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.BufferSize=20
log4j.appender.email.SMTPHost=email-smtp.us-east-1.amazonaws.com
log4j.appender.email.SMTPUsername=<username>
log4j.appender.email.SMTPPassword=<password>
log4j.appender.email.SMTPPort=587
log4j.appender.email.From=<from#email>
log4j.appender.email.To=<to#email>
log4j.appender.email.Subject=<Subjec>
log4j.appender.email.layout=org.apache.log4j.PatternLayout
log4j.appender.email.layout.ConversionPattern=%d %-5p [%t] (%c{1}:%L) - %m%n
log4j.appender.email.SMTPDebug=true
log4j.appender.email.EnableSsl=true
log4j.appender.email.smtp.starttls.enable=true
log4j.appender.email.smtp.auth=true
log4j.appender.email.TLS=true
Replace
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>
on
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>1.6.7</version>
</dependency>
Solution: I have used 1.6.4 version for mailapi and smtp jars.

Redirect java.util.logging to log4j

I'm using an external JAR library that creates a log file every time is used:
private void initLogger() {
try {
boolean var1 = true;
FileHandler var2 = new FileHandler("lib.log", 4096000, 1, var1);
var2.setFormatter(new SimpleFormatter());
this.logger = Logger.getLogger("POS");
this.logger.addHandler(var2);
} catch (IOException var3) {
var3.printStackTrace();
}
}
This, create the files lib.log , lib.log.lck, etc...
The problem is that my App is using log4j(from maven xml):
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
What I want is simple, I want to redirect all the calls of that lib log to my logger.
I've tried using jul-to-slf4j :
https://www.slf4j.org/legacy.html
<!-- https://mvnrepository.com/artifact/org.slf4j/jul-to-slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
With this configuration (console + daily file):
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1, RollingAppender
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%-5p] %d %c - %m%n
log4j.appender.RollingAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingAppender.File=pos.log
log4j.appender.RollingAppender.DatePattern='.'yyyy-MM-dd
log4j.appender.RollingAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingAppender.layout.ConversionPattern=[%-5p] %d %c - %m%n
And then put this code at the beginning:
LogManager.getLogManager().reset();
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
Logger.getLogger("POS").setLevel(FINEST);
With this, the file is created (lib.log) but it's empty. And the output doesn't go to pos.log ( the one configured in log4j).
What I'm missing? Thanks!
You did not include slf4j-log4j12 in your project (more).
Maybe Log4j JDK Logging Adapter is better solution.

What version of log4j did %c{1.} become valid

I would like to use the following Conversion Pattern
%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1.}:%L - %m%n
Which produces output like
2016-06-08 10:29:40 [http-nio-8080-exec-8] DEBUG h.d.h.l.l.s.w.f.MyClass:27 - This is a debug message.
2016-06-08 10:29:40 [http-nio-8080-exec-8] INFO h.d.h.l.l.s.w.f.MyClass:22 - This is an info message.
2016-06-08 10:29:40 [http-nio-8080-exec-8] WARN h.d.h.l.l.s.w.f.MyClass:33 - This is a warn message.
2016-06-08 10:29:40 [http-nio-8080-exec-8] ERROR h.d.h.l.l.s.w.f.MyClass:39 - This is an error message.
2016-06-08 10:29:40 [http-nio-8080-exec-8] FATAL h.d.h.l.l.s.w.f.MyClass:45 - This is a fatal message.
However when I run my tests and trigger my log4j file I get the error message
log4j:ERROR Category option "1." not a decimal integer.
Log4j and slf4j are setup in my pom with
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.19</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.19</version>
<scope>runtime</scope>
</dependency>
What version of log4j do I need to get 1. to be a valid Category option.
I was using PatternLayout not EnhancedPatternLayout
%c{1.}
Is only available in EnhancedPatternLayout

Control myBatis logs destination file and level

I'm working on a spring-based application which has to communicate with a SQL database through mybatis: all right but the logs destination.
For some reason mybatis logs to the wrong file, could you help me to figure out why? Here's my configuration:
log4j.properties:
### Appenders
# Console appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=WARN
log4j.appender.console.layout=org.apache.log4j.PatternLayout
# Application file appender
log4j.appender.main=org.apache.log4j.RollingFileAppender
log4j.appender.main.File=logs/app.log
log4j.appender.main.layout=org.apache.log4j.PatternLayout
log4j.appender.main.MaxFileSize=10MB
log4j.appender.main.MaxBackupIndex=15
# Libs file appender
log4j.appender.libs=org.apache.log4j.RollingFileAppender
log4j.appender.libs.File=logs/app_libs.log
log4j.appender.libs.layout=org.apache.log4j.PatternLayout
log4j.appender.libs.MaxFileSize=10MB
log4j.appender.libs.MaxBackupIndex=15
### Loggers & additivity
# Application
log4j.additivity.our.company.basepackage=false
log4j.logger.our.company.basepackage=TRACE,main,console
# Root logger
log4j.rootLogger=INFO,libs
pom.xml snippet
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>runtime</scope>
</dependency>
I find TRACE-level rows of mybatis ("org.apache.ibatis.logging.jdbc.BaseJdbcLogger.trace(BaseJdbcLogger.java:145)") in the file "app.log".
I excluded commons-logging from spring-core, and with a dependency tree I don't see commons-logging. Why isn't mybatis logging to the file "app_libs.log"? Why does mybatis not respect the specified level?
Thank you.
Edit 1
The code with which the database gets queried has been generated with mybatis-generator, and the generated code lives somewhere under the package "our.company.basepackage".
Since the question was posted, I didn't stop to think about this, until now: I found the reason of that behaviour.
The decisive suggestion is that "the code has been generated with mybatis-generator", and it has been generated in the same subpackage of the application: this means that the *Mapper classes, used for querying the databaes, effectively are in the application package and so their logs are treated as logs of "our.company.basepackage" and not as logs of "org.apache.ibatis".
The "org.apache.ibatis" in logs rows was misleading me.
After this small insight, I added the following to my log4j.properties:
log4j.additivity.our.company.basepackage.persistence.mybatis=false
log4j.logger.our.company.basepackage.persistence.mybatis=INFO,libs
With these 2 more lines, everything works properly, i.e. no more "org.apache.ibatis" rows in app.log.
I hope that this can be useful to someone other using mybatis-generator.

Can't disable quartz-scheduler logging using SLF4J

After adding quartz-scheduler to a project, Tomcat's server log is being spammed with the following message:
[INFO] [talledLocalContainer] 12:15:06.319
[DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG
o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
I'm trying to disable that log message, which repeats every 25-seconds or so. I've been through a number of other answers to this same question, such as:
Disable quartz logging
stop quartz debug logging log4j
Disabling Log4J Output in Java
...and none of the approaches suggested are working.
I have the following dependencies declared in pom.xml:
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
And I've added the following log4j.properties settings to my project:
log4j.rootLogger=OFF
log4j.logger.quartz=OFF
log4j.logger.o.quartz=OFF
log4j.logger.org.quartz=OFF
...and also the following simplelogger.properties:
org.slf4j.simpleLogger.defaultLogLevel=error
In addition to attempting the programmatic solution suggested on one of the linked answers, which should disable all logging and goes roughly like:
List<Logger> loggers = Collections.<Logger>list(LogManager.getCurrentLoggers());
loggers.add(LogManager.getRootLogger());
for ( Logger logger : loggers ) {
logger.setLevel(Level.OFF);
}
That seems to disable everything except the log message from quartz when it runs.
Is there a way to get rid of the log message from quartz, short of modifying the quartz source-code to remove it?
The logging entry above doesn't look like Log4J. I think thats ACL or JUL. When you are using Slf4j to Log4J, you also need to redirect these frameworks to Slf4j. Add these dependencies to your project as well:
<!--Redirect Apache Commons Logging to Slf4J -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.5</version>
</dependency>
<!--Redirect Java Util Logging to Slf4J -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.5</version>
</dependency>
Read this doc page about redirecting Jul to Slf4J, too. Be sure to exclude any existing commons-logging.jar files from your project, through Maven.
(From the Slf4J documentation page)
And if all else fails, try the following code:
//see if we can find the offending logger through slf4j's LoggerFactory
org.slf4j.Logger logger =
LoggerFactory.getLogger(Class.forName("org.quartz.core.QuartzSchedulerThread"));
if (logger != null && logger instanceof ch.qos.logback.classic.Logger) {
//the slf4j Logger interface doesn't expose any configuration API's, but
//we can cast to a class that does; so cast it and disable the logger
((ch.qos.logback.classic.Logger)logger).setLevel(
ch.qos.logback.classic.Level.OFF);
}

Categories