`
风过无声
  • 浏览: 92602 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

SLF4J-bridge

 
阅读更多

桥接器:用于将第三方库中的日志系统由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/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://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   

   

分享到:
评论

相关推荐

    log4j.jar各个版本

    log4j-over-slf4j-1.5.6-sources.jar, log4j-over-slf4j-1.5.6.jar, log4j-over-slf4j-1.5.8-sources.jar, log4j-over-slf4j-1.5.8.jar, log4j-over-slf4j-1.6.1.jar, log4j-slf4j-impl-2.0.2-javadoc.jar, log4j-...

    slf4j最新jar包下载和jar包

    SLF4J(Simple Logging Facade for Java)是Java中的一种日志抽象层,它提供了一个接口,允许用户在运行时动态地绑定到各种具体的日志框架,如Log4j、Java内置的日志或者Logback等。这个设计使得开发者可以在不修改...

    Hibernate中log4j-1.2.16 +slf4j-log4j12-1.5.2

    &lt;property name="hibernate.logging.factory_class"&gt;org.slf4j.bridge.SLF4JLoggingSessionFactory ``` 这将使Hibernate通过SLF4J输出SQL语句。 完成以上步骤后,你就可以在运行时根据配置控制日志输出,以便更...

    log4j-to-slf4j-2.0.jar

    Log4j 2 API和SLF4J之间的Apache Log4j绑定。 org.apache.logging.log4j/log4j-to-slf4j/2.0/log4j-to-slf4j-2.0.jar

    log4j-slf4j-impl-2.9.0.jar

    将Apache Log4j SLF4J API绑定到Log4j 2 Core org.apache.logging.log4j/log4j-slf4j-impl/2.9.0/log4j-slf4j-impl-2.9.0.jar

    slf4j-1.5.8.rar Hibernate日志接口

    &lt;property name="org.hibernate.logging.factory.class"&gt;org.slf4j.bridge.SLF4JLoggingLoggerFactory ``` 然后,在你的应用启动时,SLF4J会自动检测到`slf4j-log4j12-1.5.8.jar`,并使用Log4j进行日志记录。通过...

    log4j-slf4j-impl-2.0.1.jar

    将Apache Log4j SLF4J API绑定到Log4j 2 Core org.apache.logging.log4j/log4j-slf4j-impl/2.0.1/log4j-slf4j-impl-2.0.1.jar

    log4j-slf4j-impl-2.8.2.jar

    将Apache Log4j SLF4J API绑定到Log4j 2 Core org.apache.logging.log4j/log4j-slf4j-impl/2.8.2/log4j-slf4j-impl-2.8.2.jar

    log4j-to-slf4j-2.13.3.jar

    Log4j 2 API和SLF4J之间的Apache Log4j绑定。 org.apache.logging.log4j/log4j-to-slf4j/2.13.3/log4j-to-slf4j-2.13.3.jar

    log4j-slf4j-impl-2.9.1.jar

    将Apache Log4j SLF4J API绑定到Log4j 2 Core org.apache.logging.log4j/log4j-slf4j-impl/2.9.1/log4j-slf4j-impl-2.9.1.jar

    log4j-to-slf4j-2.10.0.jar

    Log4j 2 API和SLF4J之间的Apache Log4j绑定。 org.apache.logging.log4j/log4j-to-slf4j/2.10.0/log4j-to-slf4j-2.10.0.jar

    log4j-to-slf4j-2.12.1.jar

    Log4j 2 API和SLF4J之间的Apache Log4j绑定。 org.apache.logging.log4j/log4j-to-slf4j/2.12.1/log4j-to-slf4j-2.12.1.jar

    log4j-to-slf4j-2.14.1.jar

    Log4j 2 API和SLF4J之间的Apache Log4j绑定。 org.apache.logging.log4j/log4j-to-slf4j/2.14.1/log4j-to-slf4j-2.14.1.jar

    slf4j-jdk14-1.3.0.jar

    SLF4J JDK14绑定 org.slf4j/slf4j-jdk14/1.3.0/slf4j-jdk14-1.3.0.jar

    slf4j-jdk14-1.1.0.jar

    SLF4J JDK14绑定 org.slf4j/slf4j-jdk14/1.1.0/slf4j-jdk14-1.1.0.jar

    slf4j-impl-2.0-alpha1.jar

    SLF4J API和Log4J2 Core之间的绑定 org.apache.logging.log4j/slf4j-impl/2.0-alpha1/slf4j-impl-2.0-alpha1.jar

    log4j-to-slf4j-2.0-rc2.jar

    Log4j 2 API和SLF4J之间的Apache Log4j绑定。 org.apache.logging.log4j/log4j-to-slf4j/2.0-rc2/log4j-to-slf4j-2.0-rc2.jar

    jul-to-slf4j-stub-1.0.1.Final.jar

    7月转SLF4J存根 org.jboss.logging/jul-to-slf4j-stub/1.0.1.Final/jul-to-slf4j-stub-1.0.1.Final.jar

    log4j-to-slf4j-2.14.0.jar

    Log4j 2 API和SLF4J之间的Apache Log4j绑定。 org.apache.logging.log4j/log4j-to-slf4j/2.14.0/log4j-to-slf4j-2.14.0.jar

    log4j-to-slf4j-2.12.0.jar

    Log4j 2 API和SLF4J之间的Apache Log4j绑定。 org.apache.logging.log4j/log4j-to-slf4j/2.12.0/log4j-to-slf4j-2.12.0.jar

Global site tag (gtag.js) - Google Analytics