桥接器:用于将第三方库中的日志系统由JCL,log4j和JUL重定向到slf4j中,以便日志的统一管理。

实例:将spring的日志由JCL重定向到slf4j

1)spring配置JCL+log4j

-pom.xml

    <dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>3.2.13.RELEASE</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency></dependencies>

-log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"><appender name="console" class="org.apache.log4j.ConsoleAppender"><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern"value="[%d{yyyy-MM-dd HH:mm:ss,SSS\} %-5p] [%t] %c{2\} - %m%n" />       </layout></appender><root><priority value="INFO"/><appender-ref ref="console"/></root></log4j:configuration>

-ApplicationContext-Bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean id="person" class="com.siyuan.test.slf4j.entity.Person"><property name="name" value="siyuan"/></bean></beans>

-Person.java

package com.siyuan.test.slf4j.entity;public class Person {private String name;public Person() {}public String getName() {return name;}public void setName(String name) {this.name = name;}}

-BridgeTest.java

package com.siyuan.test.slf4j.bridge;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.siyuan.test.slf4j.entity.Person;public class BridgeTest {public static void main(String[] args) {ApplicationContext ctxt = new ClassPathXmlApplicationContext("ApplicationContext-Bean.xml");System.out.println(ctxt.getBean("person"));System.out.println(((Person) ctxt.getBean("person")).getName());}}

-运行结果

[2015-06-03 00:01:40,221 INFO ] [main] support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@19c247a0: startup date [Wed Jun 03 00:01:40 CST 2015]; root of context hierarchy
[2015-06-03 00:01:40,272 INFO ] [main] xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [ApplicationContext-Bean.xml]
[2015-06-03 00:01:40,397 INFO ] [main] support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@420f9c40: defining beans [person]; root of factory hierarchy
com.siyuan.test.slf4j.entity.Person@276a38b5
siyuan

2)重定向到slf4j

-去除JCL+log4j,加入桥接器+logback

pom.xml

<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>3.2.13.RELEASE</version><exclusions><exclusion><artifactId>commons-logging</artifactId><groupId>commons-logging</groupId></exclusion></exclusions></dependency><dependency><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId><version>1.7.12</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.1.3</version></dependency></dependencies>

logback-test.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration><appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern></encoder></appender><root level="debug"><appender-ref ref="STDOUT" /></root></configuration>

BridgeTest运行结果

2015-06-03 00:07:14,556 DEBUG [main] o.s.c.e.StandardEnvironment [MutablePropertySources.java:108] Adding [systemProperties] PropertySource with lowest search precedence
2015-06-03 00:07:14,563 DEBUG [main] o.s.c.e.StandardEnvironment [MutablePropertySources.java:108] Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-03 00:07:14,564 DEBUG [main] o.s.c.e.StandardEnvironment [AbstractEnvironment.java:126] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
2015-06-03 00:07:14,568 INFO [main] o.s.c.s.ClassPathXmlApplicationContext [AbstractApplicationContext.java:512] Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@b6e39f: startup date [Wed Jun 03 00:07:14 CST 2015]; root of context hierarchy
2015-06-03 00:07:14,606 DEBUG [main] o.s.c.e.StandardEnvironment [MutablePropertySources.java:108] Adding [systemProperties] PropertySource with lowest search precedence
2015-06-03 00:07:14,607 DEBUG [main] o.s.c.e.StandardEnvironment [MutablePropertySources.java:108] Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-03 00:07:14,607 DEBUG [main] o.s.c.e.StandardEnvironment [AbstractEnvironment.java:126] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
2015-06-03 00:07:14,617 INFO [main] o.s.b.f.x.XmlBeanDefinitionReader [XmlBeanDefinitionReader.java:316] Loading XML bean definitions from class path resource [ApplicationContext-Bean.xml]
2015-06-03 00:07:14,620 DEBUG [main] o.s.b.f.x.DefaultDocumentLoader [DefaultDocumentLoader.java:72] Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]
2015-06-03 00:07:14,644 DEBUG [main] o.s.b.f.x.PluggableSchemaResolver [PluggableSchemaResolver.java:140] Loading schema mappings from [META-INF/spring.schemas]
2015-06-03 00:07:14,648 DEBUG [main] o.s.b.f.x.PluggableSchemaResolver [PluggableSchemaResolver.java:146] Loaded schema mappings: {http://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-3.2.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-3.1.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd, http://www.springframework.org/schema/task/spring-task.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd, http://www.springframework.org/schema/cache/spring-cache.xsd=org/springframework/cache/config/spring-cache-3.2.xsd, http://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd, http://www.springframework.org/schema/task/spring-task-3.1.xsd=org/springframework/scheduling/config/spring-task-3.1.xsd, http://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd, http://www.springframework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd, http://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd, http://www.springframework.org/schema/tool/spring-tool-3.1.xsd=org/springframework/beans/factory/xml/spring-tool-3.1.xsd, http://www.springframework.org/schema/jee/spring-jee-3.1.xsd=org/springframework/ejb/config/spring-jee-3.1.xsd, http://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-3.2.xsd, http://www.springframework.org/schema/context/spring-context-3.2.xsd=org/springframework/context/config/spring-context-3.2.xsd, http://www.springframework.org/schema/util/spring-util-3.2.xsd=org/springframework/beans/factory/xml/spring-util-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang-3.2.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd, http://www.springframework.org/schema/cache/spring-cache-3.2.xsd=org/springframework/cache/config/spring-cache-3.2.xsd, http://www.springframework.org/schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd, http://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd, http://www.springframework.org/schema/tool/spring-tool-3.0.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd, http://www.springframework.org/schema/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd, http://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd, http://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd, http://www.springframework.org/schema/aop/spring-aop-3.2.xsd=org/springframework/aop/config/spring-aop-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-3.0.xsd=org/springframework/ejb/config/spring-jee-3.0.xsd, http://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd, http://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/spring-context-3.1.xsd, http://www.springframework.org/schema/util/spring-util-3.1.xsd=org/springframework/beans/factory/xml/spring-util-3.1.xsd, http://www.springframework.org/schema/lang/spring-lang-3.1.xsd=org/springframework/scripting/config/spring-lang-3.1.xsd, http://www.springframework.org/schema/cache/spring-cache-3.1.xsd=org/springframework/cache/config/spring-cache-3.1.xsd, http://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd, http://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd, http://www.springframework.org/schema/beans/spring-beans-3.2.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd, http://www.springframework.org/schema/aop/spring-aop-3.1.xsd=org/springframework/aop/config/spring-aop-3.1.xsd, http://www.springframework.org/schema/task/spring-task-3.2.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd, http://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd, http://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-3.2.xsd, http://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd, http://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd, http://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd, http://www.springframework.org/schema/tool/spring-tool-3.2.xsd=org/springframework/beans/factory/xml/spring-tool-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd}
2015-06-03 00:07:14,650 DEBUG [main] o.s.b.f.x.PluggableSchemaResolver [PluggableSchemaResolver.java:118] Found XML schema [http://www.springframework.org/schema/beans/spring-beans.xsd] in classpath: org/springframework/beans/factory/xml/spring-beans-3.2.xsd
2015-06-03 00:07:14,719 DEBUG [main] o.s.b.f.x.DefaultBeanDefinitionDocumentReader [DefaultBeanDefinitionDocumentReader.java:99] Loading bean definitions
2015-06-03 00:07:14,739 DEBUG [main] o.s.b.f.x.XmlBeanDefinitionReader [AbstractBeanDefinitionReader.java:216] Loaded 1 bean definitions from location pattern [ApplicationContext-Bean.xml]
2015-06-03 00:07:14,739 DEBUG [main] o.s.c.s.ClassPathXmlApplicationContext [AbstractApplicationContext.java:542] Bean factory for org.springframework.context.support.ClassPathXmlApplicationContext@b6e39f: org.springframework.beans.factory.support.DefaultListableBeanFactory@67eb366: defining beans [person]; root of factory hierarchy
2015-06-03 00:07:14,756 DEBUG [main] o.s.c.s.ClassPathXmlApplicationContext [AbstractApplicationContext.java:809] Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@2c09505f]
2015-06-03 00:07:14,760 DEBUG [main] o.s.c.s.ClassPathXmlApplicationContext [AbstractApplicationContext.java:833] Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@205ddb6e]
2015-06-03 00:07:14,761 INFO [main] o.s.b.f.s.DefaultListableBeanFactory [DefaultListableBeanFactory.java:603] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@67eb366: defining beans [person]; root of factory hierarchy
2015-06-03 00:07:14,762 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory [DefaultSingletonBeanRegistry.java:215] Creating shared instance of singleton bean 'person'
2015-06-03 00:07:14,762 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory [AbstractAutowireCapableBeanFactory.java:432] Creating instance of bean 'person'
2015-06-03 00:07:14,781 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory [AbstractAutowireCapableBeanFactory.java:506] Eagerly caching bean 'person' to allow for resolving potential circular references
2015-06-03 00:07:14,816 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory [AbstractAutowireCapableBeanFactory.java:460] Finished creating instance of bean 'person'
2015-06-03 00:07:14,817 DEBUG [main] o.s.c.s.ClassPathXmlApplicationContext [AbstractApplicationContext.java:860] Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@3801318b]
2015-06-03 00:07:14,817 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory [AbstractBeanFactory.java:243] Returning cached instance of singleton bean 'lifecycleProcessor'
2015-06-03 00:07:14,821 DEBUG [main] o.s.c.e.PropertySourcesPropertyResolver [PropertySourcesPropertyResolver.java:81] Searching for key 'spring.liveBeansView.mbeanDomain' in [systemProperties]
2015-06-03 00:07:14,821 DEBUG [main] o.s.c.e.PropertySourcesPropertyResolver [PropertySourcesPropertyResolver.java:81] Searching for key 'spring.liveBeansView.mbeanDomain' in [systemEnvironment]
2015-06-03 00:07:14,822 DEBUG [main] o.s.c.e.PropertySourcesPropertyResolver [PropertySourcesPropertyResolver.java:103] Could not find key 'spring.liveBeansView.mbeanDomain' in any property source. Returning [null]
2015-06-03 00:07:14,822 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory [AbstractBeanFactory.java:243] Returning cached instance of singleton bean 'person'
com.siyuan.test.slf4j.entity.Person@26d66426
2015-06-03 00:07:14,822 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory [AbstractBeanFactory.java:243] Returning cached instance of singleton bean 'person'
siyuan

log4j和JUL的重定向也类似

参考资料

http://www.slf4j.org/legacy.html

SLF4J-bridge相关推荐

  1. logback slf4j_强制Tomcat通过SLF4J / Logback登录

    logback slf4j 因此,您将JAR可执行Web应用程序与Tomcat捆绑在一起 (请务必先阅读其中一个). 但是,一开始就有这些烦人的Tomcat日志,它们独立于我们的应用程序日志且不可自定 ...

  2. 强制Tomcat通过SLF4J / Logback登录

    因此,您将JAR可执行Web应用程序与Tomcat捆绑在一起 (请务必先阅读其中一个). 但是,开头有这些烦人的Tomcat日志,与我们的应用程序日志无关,并且不可自定义: Nov 24, 2012 ...

  3. SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelecto

    标签: Java日志方案有很多,包括:java.util.logging.Apache的commons-logging和log4j.slf4j以及logback. 一个大型项目会用到众多第三方jar包 ...

  4. Java日志系统概述SLF4J、log4j、JCL、Logback

    java日志系统经常遇到SLF4j,JCL,logback,log4j2等等.一些人可能要晕了怎么选择,这里简单说下. 发展 这些都要从Java日志框架的元老log4j说起.java1.3之前打日志都 ...

  5. Elasticsearch Java API 6.2(java client)

    前言 本节描述了Elasticsearch提供的Java API,所有的Elasticsearch操作都使用客户端对象执行,所有操作本质上都是完全异步的(要么接收监听器,要么未来返回). 此外,客户端 ...

  6. 探索java世界中的日志奥秘

    java日志简单介绍 对于一个应用程序来说日志记录是必不可少的一部分.线上问题追踪,基于日志的业务逻辑统计分析等都离不日志.JAVA领域存在多种日志框架,目前常用的日志框架包括Log4j,Log4j ...

  7. flatmap_flatMap()与concatMap()与concatMapEager()– RxJava常见问题解答

    flatmap RxJava 2.x中共有三个无缝相似的运算符: flatMap() , concatMap()和concatMapEager() . 它们都接受相同的参数-从原始流的单个项目到任意类 ...

  8. flatMap()与concatMap()与concatMapEager()– RxJava常见问题解答

    RxJava 2.x中共有三个无缝相似的运算符: flatMap() , concatMap()和concatMapEager() . 它们都接受相同的参数-从原始流的单个项目到任意类型的(子)流的函 ...

  9. Java操作Hive

    Hadoop版本:hadoop-2.9.2.tar.gz,Hive版本:apache-hive-2.3.6-src.tar.gz,安装Hive可查看:CentOS安装Hive 保证Hive以正确启动h ...

  10. 解决hbase报错Master exiting

    使用版本: hadoop-2.10.1 hbase-2.4.15 执行命令start-hbase.sh后主进程Hmaster和从进程HRegionServer都没有启动.查看日志报错如下: 可以看到具 ...

最新文章

  1. Python--day5--常用模块
  2. html转chm后脚本错误,win10系统打开chm资料弹出窗口提示脚本出现错误的恢复办法...
  3. 并发编程基础之volatile关键字的用法
  4. caffe.pb.h丢失问题:
  5. mysql中 !40000 DROP DATABASE IF EXISTS `top_server` 这中注释有什么作用?
  6. logging模块和包
  7. Unity性能优化的N种武器
  8. 【学习笔记】第一章——操作系统的中断和异常
  9. java构造方法 this_Java中的构造方法this、super的用法详解
  10. js 页面载入时的执行顺序
  11. 值得收藏的图片网站,设计素材不愁,还能承包你一年壁纸
  12. mysql中字符串处理替换字符replace和连接字符串函数concat
  13. python idle连接失败_不能通过IDLE从Explorer运行Python [2013] - IDLE的子进程没有连接...
  14. 机械秒表的使用方法_秒表的使用方法?
  15. android 基带版本,手机基带是什么?手机的基带版本是什么意思?
  16. svchost.exe程序下载解决方法或者在360中看到svchost.exe占网速
  17. python的split函数作用_spilt函数 详解 for Python
  18. 9、Cascaded Diffusion Models for High Fidelity Image Generation
  19. 求职真的是欲哭无泪,520,521还要继续找工作
  20. python画旺仔代码_python基础1

热门文章

  1. 【LOJ 3037】开关游戏(DP)
  2. html5 箭头形状导航条,css实现带箭头的导航条
  3. 【疑难杂症04】EOFException异常详解
  4. 安全散列算法(SHA、SHA1)简述
  5. 【JAVA进阶】static关键字详解
  6. 大学物理实验----单摆,实验数据处理。C语言代码实现
  7. python 规则引擎 drools_Drools规则引擎决策表使用场景
  8. CSS从入门到喜欢,从喜欢到着魔
  9. 密码库LibTomCrypt学习记录——(2.11)分组密码算法的工作模式——CTR代码示例
  10. Visual C++ 6.0 写一个简单的程序