commons-loggin 与 slf4j的桥接器

选择使用SLF4j + Logback后,像Spring等还未使用SLF4j作为日志接口的第三方类库和工具,不能共享Logback做为底层日志工具,通过以下对JCL的扩展 则可达到使用Logback的目的:

 

package org.apache.commons.logging.impl.ext;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author inc062805
 * 
 * 该类是commons-loggin 与 slf4j的桥接类
 */
public class LogbackLogger implements Log, Serializable {
	//
	private static final long serialVersionUID = -5610674106929984260L;

	//
	private transient Logger logger = null;

	private String name = null;

	public LogbackLogger() {
	}

	public LogbackLogger(String name) {
		this.name = name;
		logger = getLogger();
	}

	/**
	 * Return the native Logger instance we are using.
	 */
	public Logger getLogger() {
		if (logger == null) {
			logger = LoggerFactory.getLogger(name);
		}
		return logger;
	}

	public void debug(Object msg) {
		getLogger().debug(msg.toString());
	}

	public void debug(Object msg, Throwable ex) {
		getLogger().debug(msg.toString(), ex);

	}

	public void error(Object msg) {
		getLogger().error(msg.toString());

	}

	public void error(Object msg, Throwable ex) {
		getLogger().error(msg.toString(), ex);

	}

	public void fatal(Object msg) {
		error(msg);
	}

	public void fatal(Object msg, Throwable ex) {
		error(msg, ex);
	}

	public void info(Object msg) {
		getLogger().info(msg.toString());
	}

	public void info(Object msg, Throwable ex) {
		getLogger().info(msg.toString(), ex);
	}

	public void trace(Object msg) {
		getLogger().trace(msg.toString());
	}

	public void trace(Object msg, Throwable ex) {
		getLogger().trace(msg.toString(), ex);
	}

	public void warn(Object msg) {
		getLogger().warn(msg.toString());
	}

	public void warn(Object msg, Throwable ex) {
		getLogger().warn(msg.toString(), ex);
	}

	public boolean isDebugEnabled() {
		return getLogger().isDebugEnabled();
	}

	public boolean isErrorEnabled() {
		return getLogger().isErrorEnabled();
	}

	public boolean isFatalEnabled() {
		return getLogger().isErrorEnabled();
	}

	public boolean isInfoEnabled() {
		return getLogger().isInfoEnabled();
	}

	public boolean isTraceEnabled() {
		return getLogger().isTraceEnabled();
	}

	public boolean isWarnEnabled() {
		return getLogger().isWarnEnabled();
	}
}

 

你也可以从附件中下载以上类文件。

你可能感兴趣的:(apache,spring,ext)