Hi everybody.
After try a lot of solutions/testing, I come to ask for help.
I've a Java Program, that use spring-social and mongoDB.
However, it's polluted by Spring and Mongo logs :
[main] INFO org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2f0a87b3: startup date [Wed Mar 22 11:37:21 CET 2017]; root of context hierarchy
[main] INFO org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
[main] INFO org.mongodb.driver.cluster - Cluster created with settings {hosts=[ds19220.mlab.com:19220], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
[cluster-ClusterId{value='58d253e1bbc0823c8d76f9ac', description='null'}-ds189220.mlab.com:19220] INFO org.mongodb.driver.connection - Opened connection [connectionId{localValue:1, serverValue:216872}] to ds189220.mlab.com:19220
[cluster-ClusterId{value='58d253e1bbc0823c8d76f9ac', description='null'}-ds189220.mlab.com:19220] INFO org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=ds189220.mlab.com:19220, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 12]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=18037014, setName='rs-ds189220', canonicalAddress=ds189220-a.mlab.com:19220, hosts=[ds189220-a.mlab.com:19220], passives=[], arbiters=[], primary='ds189220-a.mlab.com:19220', tagSet=TagSet{[]}, electionId=7fffffff0000000000000001, setVersion=1, lastWriteDate=null, lastUpdateTimeNanos=8721874212220}
[main] INFO org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor - No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
[INFO ] - ================================
[INFO ] - STARTING MyProgram...
[INFO ] - ================================
[INFO ] - Running MyProgramTask
[pool-2-thread-1] INFO org.mongodb.driver.connection - Opened connection [connectionId{localValue:2, serverValue:216873}] to ds189220.mlab.com:19220
[INFO ] - my logs....
I use a log4j2 XML file :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF">
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %c{1} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="org.springframework" level="OFF" additivity="false"/>
<Logger name="org.springframework.*" level="OFF" additivity="false"/>
<Logger name="org.mongodb" level="OFF" additivity="false">
<Logger name="org.mongodb.driver" level="OFF" additivity="false"/>
<Logger name="org.mongodb.driver.cluster" level="OFF" additivity="false"/>
<Root level="WARN">
<AppenderRef ref="CONSOLE"/>
</Root>
</Loggers>
</Configuration>
I add my pom.xml, maybe there is something interesting in it... :
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>MyProgram</name>
<artifactId>MyProgram</artifactId>
<groupId>java.myprogram</groupId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-twitter</artifactId>
<version>1.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.10.1.RELEASE</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src/main/res</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>com.heroku.sdk</groupId>
<artifactId>heroku-maven-plugin</artifactId>
<version>1.1.4</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
In case of, I add the main :
import java.io.IOException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.stereotype.Component;
import main.java.myprogram.config.AppConfig;
import main.java.myprogram.config.MongoConfig;
@Component
public class MyProgram implements ApplicationListener<ContextStartedEvent>{
private static final Logger log = LogManager.getRootLogger();
@SuppressWarnings("resource")
public static void main(String[] args) {
try {
new AnnotationConfigApplicationContext(AppConfig.class, MongoConfig.class).start();
} catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onApplicationEvent(ContextStartedEvent event) {
log.info("================================");
log.info("STARTING Myprogram...");
log.info("================================");
}
}
Concerning log4j2, i've tried to use WARN/ERROR/OFF but without success.
Moreover, I've already test to use lo4j1 and java.util.Logging...
Maybe a spring dependency that force to print his logs ?
Anyway, even if it's not the problem of the year, thanks a lot for your help.
---------------------------------------------
EDIT : According to @wangyuntao, after adding a the JCL bridge (and the SLF4J because of Mongodb driver), everything is OK !
New pom dependencies :
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-twitter</artifactId>
<version>1.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.10.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.2</version>
</dependency>
<!-- LOGS -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.8.1</version>
</dependency>
As spring document says, spring uses Commons Logging for all internal logging:
Spring Boot uses Commons Logging for all internal logging, but leaves the underlying log implementation open. Default configurations are provided for Java Util Logging, Log4J2 and Logback. In each case loggers are pre-configured to use console output with optional file output also available.
So I think you should use log4j2 Commons Logging Bridge to route spring logs to log4j2.
The Commons Logging Bridge allows applications coded to the Commons Logging API to use Log4j 2 as the implementation.
Usage
Using the Commons Logging Bridge is straightforward. Simply add the bridge jar along with the other Log4j 2 jars and the Commons Logging jar, and all logging done using the Commons Logging API will be routed to Log4j.
Please refer to the following links for more details.