ibatis里面日志记录支持多种日志就起原理
ibatis里面提供了四中日志记录器,一种是apache的common loggin,一种是jdk的日志记录
一种是log4j,一种是自己实现的不记录。
那么ibats是怎么来实现不同种类来切换的呢?通过顺序加载每一种日志记录器来实现,也就是如果classpath下有哪种日志记录器的jar,就用哪种来实现,代码如下:
public class LogFactory { private static Constructor logConstructor; static { tryImplementation("org.apache.commons.logging.LogFactory", "com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl"); tryImplementation("org.apache.log4j.Logger", "com.ibatis.common.logging.log4j.Log4jImpl"); tryImplementation("java.util.logging.Logger", "com.ibatis.common.logging.jdk14.Jdk14LoggingImpl"); tryImplementation("java.lang.Object", "com.ibatis.common.logging.nologging.NoLoggingImpl"); } private static void tryImplementation(String testClassName, String implClassName) { if (logConstructor == null) { try { Resources.classForName(testClassName); Class implClass = Resources.classForName(implClassName); logConstructor = implClass.getConstructor(new Class[]{Class.class}); } catch (Throwable t) { } } }