Spring 不允许删除一些日志 (Mongo, Spring-Social-Twitter,...)
Spring does not allow to delete some logs (Mongo, Spring-Social-Twitter,...)
大家好。
试了很多solutions/testing,来求助
我有一个 Java 程序,它使用 spring-social 和 mongoDB。
但是,它被 Spring 和 Mongo 日志污染了:
[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....
我使用 log4j2 XML 文件 :
<?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>
我添加我的pom.xml,也许里面有一些有趣的东西......:
<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>
万一,我主要加上:
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("================================");
}
}
关于 log4j2,我尝试使用 WARN/ERROR/OFF 但没有成功。
此外,我已经测试过使用 lo4j1 和 java.util.Logging...
也许 spring 强制打印他的日志的依赖项?
总之,就算不是当年的问题,也非常感谢您的帮助。
---------------------------------------- -----
编辑 :根据@wangyuntao 的说法,在添加 JCL 桥接器(和 SLF4J 因为 Mongodb 驱动程序)之后,一切正常!
新 pom 依赖项:
<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>
正如 spring 文档所述,spring 使用 Commons 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.
所以我认为您应该使用 log4j2 Commons Logging Bridge 将 spring 日志路由到 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.
详情请参考以下链接。
大家好。
试了很多solutions/testing,来求助
我有一个 Java 程序,它使用 spring-social 和 mongoDB。
但是,它被 Spring 和 Mongo 日志污染了:
[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....
我使用 log4j2 XML 文件 :
<?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>
我添加我的pom.xml,也许里面有一些有趣的东西......:
<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>
万一,我主要加上:
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("================================");
}
}
关于 log4j2,我尝试使用 WARN/ERROR/OFF 但没有成功。
此外,我已经测试过使用 lo4j1 和 java.util.Logging...
也许 spring 强制打印他的日志的依赖项?
总之,就算不是当年的问题,也非常感谢您的帮助。
---------------------------------------- -----
编辑 :根据@wangyuntao 的说法,在添加 JCL 桥接器(和 SLF4J 因为 Mongodb 驱动程序)之后,一切正常!
新 pom 依赖项:
<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>
正如 spring 文档所述,spring 使用 Commons 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.
所以我认为您应该使用 log4j2 Commons Logging Bridge 将 spring 日志路由到 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.
详情请参考以下链接。