SLF4J-migrator

JAVA工具包,能自动将源代码中的日志系统commons-logging,log4j,java.util.logging迁移到slf4j上。

实例,将日志系统由commons-logging迁移到slf4j。

 1)commons-logging使用及配置

-pom.xml

		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.3</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>

-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="debug"/>
        <appender-ref ref="console"/>
    </root>
    
</log4j:configuration>

-MigratorTest.java

package com.siyuan.test.slf4j.migrator;

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

public class MigratorTest {
	
	public static void main(String[] args) {
		Log logger = LogFactory.getLog(MigratorTest.class);
		logger.debug("migrator test for slf4j");
	}
	
}

-运行结果

[2015-06-02 07:25:23,977 DEBUG] [main] migrator.MigratorTest - migrator test for slf4j

2)转化为slf4j

-下载运行slf4j-migrator

SLF4J-migrator_第1张图片
-更换pom.xml中的日志系统依赖

 

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.12</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.1.3</version>
		</dependency>

-将log4j.xml更换为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>

-运行结果

 

2015-06-02 07:29:54,819 DEBUG [main] c.s.t.s.m.MigratorTest [MigratorTest.java:10] migrator test for slf4j

限制

1)打包脚本需要手动修改

2)只支持String类型的日志信息,其它对象需要手动调用toString方法

3)不支持FATAL级别

4)假如调用某个方法时使用了多个logger参数,将无法保证完全转换

限制-Log4j

Log4j特有的API将需要手动转换,e.g. NDC、PropertyConfigurator、DomConfigurator    

限制-JUL

参考资料

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

你可能感兴趣的:(slf4j)