Spring 的 Log机制

      当把spring作为独立的程序运行时,无论是在命令行里指定 -Dlog4j.configuration  还是在classpath下面防止 log4j.xml,最后发现spring中的log4j没有

使用我们指定的log4j.xml配置文件。如何修改spring使用的默认log4j的配置呢。下面这篇Spring的 Log机制就写的很清楚了。

https://spring.io/blog/2009/12/04/logging-dependencies-in-spring/


      简单说Srping使用了commons-logging-1.2 来动态发现 日志框架,我们只要在spring中禁止掉这个jar就行,在pom.xml加入exclusions

		
		   org.springframework
		   spring-context
		   ${spring-framework.version}
		   
         	      
            		commons-logging
            		commons-logging
         	      
      		    			
		
   让spring项目以来一下四个,
   
      org.slf4j
      jcl-over-slf4j
      1.7.0
      runtime
   
   
      org.slf4j
      slf4j-api
      1.7.0
      runtime
   
   
      org.slf4j
      slf4j-log4j12
      1.7.0
      runtime
   
   
      log4j
      log4j
      1.2.14
      runtime
   

用STS 创建出来的Srping Mamen Project还使用了 logbak,如果使用logback,那你的传入的 log4j.XML也是不会生效的,SLF4J默认回去

使用logback中的配置,命令行中会答应出来logback使用了什么配置。

java -Djava.ext.dirs=./dependency -cp .;log_test-0.0.1-SNAPSHOT.jar log_test.LogTest

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/code/Java/log_test/target/dependency/logba
ck-classic-1.0.13.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/code/Java/log_test/target/dependency/slf4j
-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorSta
ticBinder]
15:43:02.199 [main] INFO  log_test.LogTest - this is log test


    禁止logback很简单,可以在pom.xml中把依赖去掉,或者直接在 classpath中删除 logback jar包。




你可能感兴趣的:(Spring 的 Log机制)