Spring 的 Log机制

来源:互联网 发布:android网络请求 编辑:程序博客网 时间:2024/06/06 01:01

      当把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包。




阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 恰烂钱 伪阿鲁纳恰尔邦 恰的拼音 恰红楼 恰到好处爱上你 恰同学少年演员表 恰与暴君共枕眠 伪阿鲁纳恰尔邦吧 爱情恰恰恰费玉清 瓦恰公路 恰怎么读 恰同学少年意思 恰同学少年刘俊卿 恰同学少年什么意思 恰柠檬 恰同学少年表达什么意思 白日不到处青春恰自来 发掘 发掘的意思 发掘战舰 发掘兵马俑 定陵发掘的悲剧 诸葛亮墓发掘现场 发掘人才不需要考试 曾侯乙墓发掘现场 发掘人才需要考试辩论赛 某报为了发掘有温度和人情味 深圳市发掘科技有限公司 秦公一号大墓发掘 三星堆为何停止发掘 刘邦墓为什么不发掘 汉武帝墓为什么不发掘 发文字号 发文 苏交科发文澄清 王九龙发文怼私生 余生导演发文斥私生 具惠善疑发文道别 两年前发文担心高架桥安全 赖冠霖发文警告 比伯妈妈开心发文