package com.zzq.logging; /** * 公共日志模型接口 * @author zzq * */ public interface Log { public void warn(String msg); public void error(String msg); public void error(String msg, Throwable t); public void debug(String msg); public void info(String msg); public boolean isDebugEnabled(); }
package com.zzq.logging; import java.lang.reflect.Constructor; public class LogFactory { private static Constructor currentLogConstructor; static { registerLog("org.apache.commons.logging.LogFactory", "com.zzq.logging.jakarta.JakartaCommonsLoggingImpl"); registerLog("org.apache.log4j.Logger", "com.zzq.logging.log4j.Log4jImpl"); registerLog("java.util.logging.Logger", "com.zzq.logging.jdk14.JDK14LoggerImpl"); registerLog("java.lang.Object", "com.zzq.logging.mylog.MyLogImpl"); } protected static void registerLog(String interfaceClass, String implClass) { if(null == currentLogConstructor) { try { Class.forName(interfaceClass); currentLogConstructor = Class.forName(implClass).getConstructor(new Class[] {Class.class}); } catch (Throwable e) { } } } public static Log getLog(Class clazz) { try { return (Log)currentLogConstructor.newInstance(new Object[] {clazz}); } catch (Throwable e) { throw new RuntimeException("创建Log实例时失败!", e); } } }
package com.zzq.logging.jakarta; import org.apache.commons.logging.LogFactory; import com.zzq.logging.Log; /** * Commons-Logging实现 * @author zzq * */ public class JakartaCommonsLoggingImpl implements Log { private org.apache.commons.logging.Log log; public JakartaCommonsLoggingImpl(Class clazz) { log = LogFactory.getLog(clazz); } public void debug(String msg) { log.debug(msg); } public void error(String msg) { log.error(msg); } public void error(String msg, Throwable t) { log.error(msg, t); } public void info(String msg) { log.info(msg); } public boolean isDebugEnabled() { return log.isDebugEnabled(); } public void warn(String msg) { log.warn(msg); } }
import java.util.logging.Level; import java.util.logging.Logger; import com.zzq.logging.Log; /** * JDK1.4 Logger实现 * @author zzq * */ public class JDK14LoggerImpl implements Log { private Logger log; public JDK14LoggerImpl(Class clazz) { log = Logger.getLogger(clazz.getName()); } public void debug(String msg) { log.log(Level.FINE, msg); } public void error(String msg) { log.log(Level.SEVERE, msg); } public void error(String msg, Throwable t) { log.log(Level.SEVERE, msg, t); } public void info(String msg) { log.log(Level.INFO, msg); } public boolean isDebugEnabled() { return log.isLoggable(Level.FINE); } public void warn(String msg) { log.log(Level.WARNING, msg); } }
import org.apache.log4j.Logger; import com.zzq.logging.Log; /** * Log4j实现 * @author zzq * */ public class Log4jImpl implements Log { private Logger log; public Log4jImpl(Class clazz) { log = Logger.getLogger(clazz); } public void debug(String msg) { log.debug(msg); } public void error(String msg) { log.error(msg); } public void error(String msg, Throwable t) { log.error(msg, t); } public void info(String msg) { log.info(msg); } public boolean isDebugEnabled() { return log.isDebugEnabled(); } public void warn(String msg) { log.warn(msg); } }
package com.zzq.logging.mylog; import java.text.SimpleDateFormat; import java.util.Date; import com.zzq.logging.Log; /** * 没有Commons-logger、log4j并且JDK版本在1.4一下会使用此类 * @author zzq * */ public class MyLogImpl implements Log { private Class clazz; private SimpleDateFormat sdf; public MyLogImpl(Class clazz) { this.clazz = clazz; sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } public void debug(String msg) { System.out.println("Debug=[" + sdf.format(new Date()) + "]" + msg + " : " + clazz.getName()); } public void error(String msg) { System.out.println("Error=[" + sdf.format(new Date()) + "]" + msg + " : " + clazz.getName()); } public void error(String msg, Throwable t) { System.out.println("Error=[" + sdf.format(new Date()) + "]" + msg + " : " + clazz.getName()); t.printStackTrace(); } public void info(String msg) { System.out.println("Info=[" + sdf.format(new Date()) + "]" + msg + " : " + clazz.getName()); } public boolean isDebugEnabled() { return false; } public void warn(String msg) { System.out.println("Warn=[" + sdf.format(new Date()) + "]" + msg + " : " + clazz.getName()); } }