speaking about logging,most people just choose one to use,JUL,Log4J,or SLF4J,but there are some complicated situation that could cause problems
you could just use log4J in your project,but if the component you dependent use others.so if you develop a component or framework,you better use apache common-logging(JCL),it provide a interface for logging,so it will enough for developing,but at run time,you need provide a implementation,such as SLF4J or log4j.
when we use third party component,such as spring framework,we want use log4J or slf4j,but,we need exclude JCL,and binding SLF4J with the log4J,so we can use log4J in our code,or if we do not binding slf4j with log4j,we can just use slf4J in our code.
here is the example that we can use slf4j or log4J in our code,but component we use is spring,commented part is using slf4J,we can put log configuration in one place,both spring and our code use same configuration and both logging correctly
package com.rogers;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Hello world!
*
*/
public class App {
//private static Logger log = LoggerFactory.getLogger(App.class);
private static Logger log = Logger.getLogger(App.class);
public static void main( String[] args )
{
log.info("hello,world---slf4j");
System.out.println( "Hello World!" );
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"application.xml"});
BO bo = ctx.getBean("bo",BO.class);
System.out.println("you name:"+bo.getName());
}
}
and here is the pom that config logging
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rogers</groupId>
<artifactId>springTest</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>springTest</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.0.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>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
No comments:
Post a Comment