/** * 日志记录器接口 * * @author yourname (mailto:[email protected]) */ public interface ILogger { boolean isDebugEnabled(); boolean isInfoEnabled(); boolean isWarnEnabled(); boolean isErrorEnabled(); void debug(Object message); void debug(Object message, Object[] params); void debug(Throwable t); void debug(Object message, Throwable t); void debug(Object message, Object[] params, Throwable t); void info(Object message); void info(Object message, Object[] params); void info(Throwable t); void info(Object message, Throwable t); void info(Object message, Object[] params, Throwable t); void warn(Object message); void warn(Object message, Object[] params); void warn(Throwable t); void warn(Object message, Throwable t); void warn(Object message, Object[] params, Throwable t); void error(Object message); void error(Object message, Object[] params); void error(Throwable t); void error(Object message, Throwable t); void error(Object message, Object[] params, Throwable t); } /** * 日志工厂 * * @author yourname (mailto:[email protected]) */ public class DebugLoggerFactory { private static LoggerFactoryPlugin logFactory = new LoggerFactoryPlugin(null); public static void setLoggerProvider(ILoggerProvider provider) { synchronized (DebugLoggerFactory.class) { logFactory = new LoggerFactoryPlugin(provider); } } /** * 动态刷新 * */ public static void refresh() { logFactory.refresh(); } /** * 取得日志记录器 * * @param clazz * @return 日志记录器 */ public static ILogger getLogger(Class clazz) { return logFactory.getLogger(clazz); } /** * 取得日志记录器 * * @param loggerName 日志名称 * @return 日志记录器 */ public static ILogger getLogger(String loggerName) { return logFactory.getLogger(loggerName); } } /** * 日志提供商 * * @author yourname (mailto:[email protected]) */ public interface ILoggerProvider { /** * 取得日志实例 * * @param loggerName 日志名称 * @return 日志实例 */ ILogger createLogger(String loggerName); } /** * 日志抽象实现 * * @author yourname (mailto:[email protected]) */ public abstract class AbstractLogger implements ILogger { public void debug(Object message) { if (message != null && message instanceof Throwable) { debug(null, null, (Throwable)message); return; } debug(message, null, null); } public void debug(Object message, Object[] params) { debug(message, params, null); } public void debug(Throwable t) { debug(null, null, t); } public void debug(Object message, Throwable t) { debug(message, null, t); } public void debug(Object message, Object[] params, Throwable t) { if (isDebugEnabled()) { doDebug(formatMessage(message, params), t); } } abstract protected void doDebug(String message, Throwable t); public void info(Object message) { if (message != null && message instanceof Throwable) { info(null, null, (Throwable)message); return; } info(message, null, null); } public void info(Object message, Object[] params) { info(message, params, null); } public void info(Throwable t) { info(null, null, t); } public void info(Object message, Throwable t) { info(message, null, t); } public void info(Object message, Object[] params, Throwable t) { if (isInfoEnabled()) { doInfo(formatMessage(message, params), t); } } abstract protected void doInfo(String message, Throwable t); public void warn(Object message) { if (message != null && message instanceof Throwable) { warn(null, null, (Throwable)message); return; } warn(message, null, null); } public void warn(Object message, Object[] params) { warn(message, params, null); } public void warn(Throwable t) { warn(null, null, t); } public void warn(Object message, Throwable t) { warn(message, null, t); } public void warn(Object message, Object[] params, Throwable t) { if (isWarnEnabled()) { doWarn(formatMessage(message, params), t); } } abstract protected void doWarn(String message, Throwable t); public void error(Object message) { if (message != null && message instanceof Throwable) { error(null, null, (Throwable)message); return; } error(message, null, null); } public void error(Object message, Object[] params) { error(message, params, null); } public void error(Throwable t) { error(null, null, t); } public void error(Object message, Throwable t) { error(message, null, t); } public void error(Object message, Object[] params, Throwable t) { if (isErrorEnabled()) { doError(formatMessage(message, params), t); } } abstract protected void doError(String message, Throwable t); protected String formatMessage(Object message, Object[] params) { StringBuilder buf = new StringBuilder(); if (message != null) { if (message instanceof String) { String msg = (String) message; if (msg.trim().length() > 0) { if (params != null && params.length > 0) { message = new MessageFormat(msg).format(params); } } } buf.append(message); } return buf.toString(); } } /** * 日志工厂实现 * * @author Administrator * */ public class LoggerFactoryPlugin { private ILoggerProvider _provider = null; private WeakHashMap<String, LoggerWrapper> logWeakMap = new WeakHashMap<String, LoggerWrapper>(); private Object lock = new Object(); public LoggerFactoryPlugin(ILoggerProvider provider) { this._provider = provider; } /** * 刷新 * */ public void refresh() { for (Entry<String, LoggerWrapper> entry : logWeakMap.entrySet()) { entry.getValue().logger = doGetLogger(entry.getKey()).logger; } } /** * 取得日志记录器 * * @param clazz * @return 日志记录器 */ public ILogger getLogger(Class clazz) { return getLogger(clazz == null ? "null" : clazz.getName()); } /** * 取得日志记录器 * * @param loggerName 日志名称 * @return 日志记录器 */ public ILogger getLogger(String loggerName) { if (loggerName == null) { loggerName = "null"; } LoggerWrapper log = doGetLogger(loggerName); synchronized(lock) { logWeakMap.put(loggerName, log); } return log; } private LoggerWrapper doGetLogger(String loggerName) { ILogger log = _provider != null ? _provider.createLogger(loggerName) : null; if (!(log instanceof LoggerWrapper)) { log = new LoggerWrapper(loggerName, log); } return (LoggerWrapper)log; } } /** * 日志包裹(防止记录日志时抛出异常) * * @author yourname (mailto:[email protected]) */ public class LoggerWrapper extends AbstractLogger { ILogger logger = null; @SuppressWarnings("unused") private String name = null; public LoggerWrapper(String name, ILogger logger) { this.name = name; if (logger == null) { return; } if (logger instanceof LoggerWrapper) { this.logger = ((LoggerWrapper)logger).logger; } else { this.logger = logger; } } public boolean isDebugEnabled() { if (logger == null) { return true; } try { return logger.isDebugEnabled(); } catch (Throwable t) { return false; } } public boolean isInfoEnabled() { if (logger == null) { return true; } try { return logger.isInfoEnabled(); } catch (Throwable t) { return false; } } public boolean isWarnEnabled() { if (logger == null) { return true; } try { return logger.isWarnEnabled(); } catch (Throwable t) { return false; } } public boolean isErrorEnabled() { if (logger == null) { return true; } try { return logger.isErrorEnabled(); } catch (Throwable t) { return false; } } protected void doDebug(String message, Throwable t) { try { logger.debug(message, t); } catch (Throwable e) { try { System.out.println(convert("DEBUG", message, t)); } catch (Throwable ignore) { } } } protected void doInfo(String message, Throwable t) { try { logger.info(message, t); } catch (Throwable e) { try { System.out.println(convert(" INFO", message, t)); } catch (Throwable ignore) { } } } protected void doWarn(String message, Throwable t) { try { logger.warn(message, t); } catch (Throwable e) { try { System.err.println(convert(" WARN", message, t)); } catch (Throwable ignore) { } } } protected void doError(String message, Throwable t) { try { logger.error(message, t); } catch (Throwable e) { try { System.err.println(convert("ERROR", message, t)); } catch (Throwable ignore) { } } } private String convert(String level, Object message, Throwable t) { StringBuilder buf = new StringBuilder(); buf.append("[").append(format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")).append("]"); buf.append("[").append(level).append("]"); // if (name != null && name.trim().length() > 0) { // buf.append("[").append(name).append("]"); // } buf.append("[").append(message).append("]"); if (t != null) { String error_msg = t.getMessage(); if (error_msg != null) { buf.append("[").append(error_msg).append("]"); } buf.append("\n"); StringWriter sw = new StringWriter(); t.printStackTrace(new PrintWriter(sw)); buf.append(sw.toString()); } return buf.toString(); } private static String format(long time, String pattern) { SimpleDateFormat dateFormat = new SimpleDateFormat(); dateFormat.applyPattern(pattern); return dateFormat.format(new Date(time)); } }