归纳接口(LoggerAdapter)如下:
//============================================================================== // // @author Slive // @date 2013-6-18 // //============================================================================== package org.slive.logging; /** * Self-defining logger interface. * it can replace any loggers simply. * <ul> * <li>{@linkplain java.util.logging.Logger} * <li>{@linkplain org.slf4j.Logger} * <li>{@linkplain org.apache.log4j.Logger} * <li>{@org.apache.commons.logging.Log} * </ul> * @author Slive */ public interface LoggerAdapter { public void info(String msg); public void info(Throwable ex); public void info(String msg,Throwable ex); public void info(String msg,Object... params); public void debug(String msg,Throwable ex); public void debug(String msg); public void debug(Throwable ex); public void debug(String msg,Object... params); public void error(String msg,Throwable ex); public void error(String msg); public void error(Throwable ex); public void error(String msg,Object... params); public void warn(String msg); public void warn(Throwable ex); public void warn(String msg,Throwable ex); public void warn(String msg,Object... params); }
举例对“java.util.logging.Logger”进行适配,实现(LoggerJavaAdapter)如下:
//============================================================================== // // @author Slive // @date 2013-6-18 // //============================================================================== package org.slive.logging.java; import java.util.logging.Level; import java.util.logging.Logger; import org.slive.logging.LoggerAdapter; /** * A simple logger's adapter of <code>java.util.logging.Logger</code> * @see java.util.logging.Logger * @author Slive */ public class LoggerJavaAdapter implements LoggerAdapter { private Logger logger; public LoggerJavaAdapter(Class clazz) { logger = Logger.getLogger(clazz != null ? clazz.getName() : " "); } @Override public void debug(String msg, Throwable ex) { logger.log(Level.FINEST, msg, ex); } @Override public void debug(String msg) { logger.log(Level.FINEST, msg); } @Override public void debug(Throwable ex) { if (ex == null) { logger.log(Level.FINEST, null); } else { logger.log(Level.FINEST, ex.getMessage(), ex); } } @Override public void debug(String msg, Object... params) { logger.log(Level.FINEST, msg, params); } @Override public void error(String msg, Throwable ex) { logger.log(Level.SEVERE, msg, ex); } @Override public void error(String msg) { logger.log(Level.SEVERE, msg); } @Override public void error(Throwable ex) { if (ex == null) { logger.log(Level.SEVERE, null); } else { logger.log(Level.SEVERE, ex.getMessage(), ex); } } @Override public void error(String msg, Object... params) { logger.log(Level.SEVERE, msg, params); } @Override public void info(String msg, Throwable ex) { logger.log(Level.INFO, msg, ex); } @Override public void info(String msg) { logger.log(Level.INFO, msg); } @Override public void info(Throwable ex) { if (ex == null) { logger.log(Level.INFO, null); } else { logger.log(Level.INFO, ex.getMessage(), ex); } } @Override public void info(String msg, Object... params) { logger.log(Level.INFO, msg, params); } @Override public void warn(String msg, Throwable ex) { logger.log(Level.WARNING, msg, ex); } @Override public void warn(String msg) { logger.log(Level.WARNING, msg); } @Override public void warn(Throwable ex) { if (ex == null) { logger.log(Level.WARNING, null); } else { logger.log(Level.WARNING, ex.getMessage(), ex); } } @Override public void warn(String msg, Object... params) { logger.log(Level.WARNING, msg, params); } }
同时实现“LoggerAdapterFacory”类,如下:
//============================================================================== // // @author Slive // @date 2013-6-18 // //============================================================================== package org.slive.logging.java; import org.slive.logging.LoggerAdapter; /** * The factory of implements logger adapter.if is in need,you can rewrite it. * @author Slive */ public class LoggerAdapterFacory { public static LoggerAdapter getLoggerAdapter(Class clazz) { //TODO according to specific circumstance //such as 'LoggerJava' return new LoggerJavaAdapter(clazz); } }