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

		<dependency>
		   <groupId>org.springframework</groupId>
		   <artifactId>spring-context</artifactId>
		   <version>${spring-framework.version}</version>
		   <exclusions>
         	      <exclusion>
            		<groupId>commons-logging</groupId>
            		<artifactId>commons-logging</artifactId>
         	      </exclusion>
      		   </exclusions> 			
		</dependency>
   让spring项目以来一下四个,
   <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>jcl-over-slf4j</artifactId>
      <version>1.7.0</version>
      <scope>runtime</scope>
   </dependency>
   <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.0</version>
      <scope>runtime</scope>
   </dependency>
   <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.0</version>
      <scope>runtime</scope>
   </dependency>
   <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.14</version>
      <scope>runtime</scope>
   </dependency>

用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包。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值