MyBatis源码之日志模块
提供日志, 并提供集成第三方日志框架的能力, 具体参考类图
1. LogFactory
Log工厂类
1.1 构造方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
public static final String MARKER = "MYBATIS";
private static Constructor<? extends Log> logConstructor;
static {
tryImplementation(LogFactory::useSlf4jLogging); tryImplementation(LogFactory::useCommonsLogging); tryImplementation(LogFactory::useLog4J2Logging); tryImplementation(LogFactory::useLog4JLogging); tryImplementation(LogFactory::useJdkLogging); tryImplementation(LogFactory::useNoLogging); }
private LogFactory() { }
|
1.1.1 tryImplementation
1 2 3 4 5 6 7 8 9
| private static void tryImplementation(Runnable runnable) { if (logConstructor == null) { try { runnable.run(); } catch (Throwable t) { } } }
|
1.1.2 useSlf4jLogging
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public static void useSlf4jLogging() { setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class); }
private static void setImplementation(Class<? extends Log> implClass) { lock.lock(); try { Constructor<? extends Log> candidate = implClass.getConstructor(String.class); Log log = candidate.newInstance(LogFactory.class.getName()); if (log.isDebugEnabled()) { log.debug("Logging initialized using '" + implClass + "' adapter."); } logConstructor = candidate; } catch (Throwable t) { throw new LogException("Error setting Log implementation. Cause: " + t, t); } finally { lock.unlock(); } }
|
其它的Log对象与 Slf4j 类似, 如果需要自定义Log的实现类可以通过如下方法
1 2 3
| public static void useCustomLogging(Class<? extends Log> clazz) { setImplementation(clazz); }
|
1.2 getLog
1 2 3 4 5 6 7 8 9 10 11
| public static Log getLog(Class<?> clazz) { return getLog(clazz.getName()); }
public static Log getLog(String logger) { try { return logConstructor.newInstance(logger); } catch (Throwable t) { throw new LogException("Error creating logger for logger " + logger + ". Cause: " + t, t); } }
|
2. Log
MyBatis 的日志父接口, 实现类较多 代码简单 以Slf4举例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public interface Log {
boolean isDebugEnabled();
boolean isTraceEnabled();
void error(String s, Throwable e);
void error(String s);
void debug(String s);
void trace(String s);
void warn(String s);
}
|
2.1 Slf4jImpl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| public class Slf4jImpl implements Log {
private Log log;
public Slf4jImpl(String clazz) { Logger logger = LoggerFactory.getLogger(clazz); if (logger instanceof LocationAwareLogger) { try { logger.getClass().getMethod("log", Marker.class, String.class, int.class, String.class, Object[].class, Throwable.class); log = new Slf4jLocationAwareLoggerImpl((LocationAwareLogger) logger); return; } catch (SecurityException | NoSuchMethodException e) { } }
log = new Slf4jLoggerImpl(logger); }
@Override public boolean isDebugEnabled() { return log.isDebugEnabled(); }
@Override public boolean isTraceEnabled() { return log.isTraceEnabled(); }
@Override public void error(String s, Throwable e) { log.error(s, e); }
@Override public void error(String s) { log.error(s); }
@Override public void debug(String s) { log.debug(s); }
@Override public void trace(String s) { log.trace(s); }
@Override public void warn(String s) { log.warn(s); }
}
|