一、前言
这边基于org.apache.log4j.Logger(log4j-1.2.7.jar)和java.util.logging.Logger日志实现类JdkLogging来自定义(org.mybatis.generator.logging归属于mybatis-generator-core.1.3.4.jar)日志工厂类LogFactory,可以增强日志的功能和容错,同时融合log4j、java.util.logging.Logger等先有实现特点满足不同应用场景。
二、代码分析
1.首先定义日志抽象接口类Log,来定义标准日志常用方法,另外这边基于log4j和java.util.logging.Logger日志实现类
public abstract interface Log@b@{@b@ public abstract boolean isDebugEnabled();@b@@b@ public abstract void error(String paramString, Throwable paramThrowable);@b@@b@ public abstract void error(String paramString);@b@@b@ public abstract void debug(String paramString);@b@@b@ public abstract void warn(String paramString);@b@}
log4j实现类
import org.apache.log4j.Logger;@b@@b@public class Log4jImpl@b@ implements Log@b@{@b@ private Logger log;@b@@b@ public Log4jImpl(Class<?> clazz)@b@ {@b@ this.log = Logger.getLogger(clazz);@b@ }@b@@b@ public boolean isDebugEnabled() {@b@ return this.log.isDebugEnabled();@b@ }@b@@b@ public void error(String s, Throwable e) {@b@ this.log.error(s, e);@b@ }@b@@b@ public void error(String s) {@b@ this.log.error(s);@b@ }@b@@b@ public void debug(String s) {@b@ this.log.debug(s);@b@ }@b@@b@ public void warn(String s) {@b@ this.log.warn(s);@b@ }@b@}
JdkLogging实现类
import java.util.logging.Level;@b@import java.util.logging.LogRecord;@b@import java.util.logging.Logger;@b@@b@public class JdkLoggingImpl@b@ implements Log@b@{@b@ private Logger log;@b@@b@ public JdkLoggingImpl(Class<?> clazz)@b@ {@b@ this.log = Logger.getLogger(clazz.getName());@b@ }@b@@b@ public boolean isDebugEnabled() {@b@ return this.log.isLoggable(Level.FINE);@b@ }@b@@b@ public void error(String s, Throwable e) {@b@ LogRecord lr = new LogRecord(Level.SEVERE, s);@b@ lr.setSourceClassName(this.log.getName());@b@ lr.setThrown(e);@b@@b@ this.log.log(lr);@b@ }@b@@b@ public void error(String s) {@b@ LogRecord lr = new LogRecord(Level.SEVERE, s);@b@ lr.setSourceClassName(this.log.getName());@b@@b@ this.log.log(lr);@b@ }@b@@b@ public void debug(String s) {@b@ LogRecord lr = new LogRecord(Level.FINE, s);@b@ lr.setSourceClassName(this.log.getName());@b@@b@ this.log.log(lr);@b@ }@b@@b@ public void warn(String s) {@b@ LogRecord lr = new LogRecord(Level.WARNING, s);@b@ lr.setSourceClassName(this.log.getName());@b@@b@ this.log.log(lr);@b@ }@b@}
2.日志工厂类LogFactory、抽象日志类AbstractLogFactory,获取到log4j和java.util.logging.Logger等日志对象,这边默认获取org.apache.log4j.Logger出错异常的时候获取java.util.logging.Logger对象,代码如下
public class LogFactory@b@{@b@ private static AbstractLogFactory logFactory;@b@@b@ public static Log getLog(Class<?> clazz)@b@ {@b@ try@b@ {@b@ return logFactory.getLog(clazz);@b@ } catch (Throwable t) {@b@ throw new RuntimeException(Messages.getString("RuntimeError.21", clazz@b@ .getName(), t.getMessage()), t);@b@ }@b@ }@b@@b@ public static synchronized void forceJavaLogging()@b@ {@b@ logFactory = new JdkLoggingLogFactory(null);@b@ }@b@@b@ public static void setLogFactory(AbstractLogFactory logFactory)@b@ {@b@ logFactory = logFactory;@b@ }@b@@b@ static@b@ {@b@ try@b@ {@b@ ObjectFactory.internalClassForName("org.apache.log4j.Logger");@b@ logFactory = new Log4jLoggingLogFactory(null);@b@ } catch (Exception e) {@b@ logFactory = new JdkLoggingLogFactory(null);@b@ }@b@ }@b@@b@ private static class Log4jLoggingLogFactory@b@ implements AbstractLogFactory@b@ {@b@ public Log getLog(Class<?> clazz)@b@ {@b@ return new Log4jImpl(clazz);@b@ }@b@ }@b@@b@ private static class JdkLoggingLogFactory@b@ implements AbstractLogFactory@b@ {@b@ public Log getLog(Class<?> clazz)@b@ {@b@ return new JdkLoggingImpl(clazz);@b@ }@b@ }@b@}
抽象日志类AbstractLogFactory
public abstract interface AbstractLogFactory@b@{@b@ public abstract Log getLog(Class<?> paramClass);@b@}