Spring Framework Logging

来源:互联网 发布:网络被骗2000报警么 编辑:程序博客网 时间:2024/06/07 04:05

Reference URL: http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/overview.html

spring-core depend explicitly on commons-logging.
commons-logging has a runtime discovery algorithm that looks for other logging frameworks in well known places on the classpath 
 and uses one that it thinks is appropriate (or you can tell it which one if you need to).
 If nothing else is available you get pretty nice looking logs just from the JDK (java.util.logging or JUL for short).
Unfortunately, the runtime discovery algorithm in commons-logging is problematic.

There are basically two ways to switch off commons-logging:
a. Exclude the dependency from the spring-core module (as it is the only module that explicitly depends on commons-logging)
b. Depend on a special commons-logging dependency that replaces the library with an empty jar.

the Simple Logging Facade for Java ( SLF4J) is a better alternative to  commons-logging.
SLF4J is a cleaner dependency and more efficient at runtime than commons-logging 
because it uses compile-time bindings instead of runtime discovery of the other logging frameworks it integrates.

To use SLF4J with Spring you need to replace the commons-logging dependency with the SLF4J-JCL bridge. 

A common choice might be to bridge Spring to SLF4J, and then provide explicit binding from SLF4J to Log4J.
 You need to supply 4 dependencies (and exclude the existing commons-logging): 
 the bridge, the SLF4J API, the binding to Log4J, and the Log4J implementation itself.
 Additionally, provide it with a configuration file ( log4j.properties or log4j.xml in the root of the classpath).

POM.XML

<dependencies>    <dependency>        <groupId>org.springframework</groupId>        <artifactId>spring-core</artifactId>        <version>4.1.6.RELEASE</version>        <exclusions>            <exclusion>                <groupId>commons-logging</groupId>                <artifactId>commons-logging</artifactId>            </exclusion>        </exclusions>    </dependency>    <dependency>        <groupId>org.slf4j</groupId>        <artifactId>jcl-over-slf4j</artifactId>        <version>1.5.8</version>    </dependency>    <dependency>        <groupId>org.slf4j</groupId>        <artifactId>slf4j-api</artifactId>        <version>1.5.8</version>    </dependency>    <dependency>        <groupId>org.slf4j</groupId>        <artifactId>slf4j-log4j12</artifactId>        <version>1.5.8</version>    </dependency>    <dependency>        <groupId>log4j</groupId>        <artifactId>log4j</artifactId>        <version>1.2.14</version>    </dependency></dependencies>


0 0