Common-log 和log4j2 配合使用

Usage

Using the Commons Logging Bridge is straightforward. Simply add the bridge jar along with the other Log4j 2 jars and the Commons Logging jar, and all logging done using the Commons Logging API will be routed to Log4j.

使用教程()
pom.xml
导入核心包
<!-- log4j2 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.3</version>
</dependency>

导入链接桥
<!-- Apache Commons Logging Bridge -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.3</version>
</dependency>


App.java
package metest.log4j2;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class App {
static Log log = LogFactory.getLog(App.class);

public static void main(String[] args) {
log.debug("---------");
}
}

log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
   <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  </Console>
  </Appenders>
  <Loggers>
  <logger name = "metest.log4j2" level="debug" additivity="false">
  <AppenderRef ref="Console"/>
  </logger>
    <Root level="error">
      <AppenderRef ref="Console"/>
    </Root>
</Loggers>
</Configuration>

注意:
additivity="false" 如果不配上,消息会被打印两遍,这个功能一般用不上,因此配置为false

猜你喜欢

转载自aselidy.iteye.com/blog/2215717