JCL入门
1 2 3 4 5
| <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency>
|
默认使用JUL作为日志底层实现,当导入log4j的包的时候自动进行转换。
1 2 3 4 5 6 7 8 9 10 11
| @Test public void testQuick(){ Log log = LogFactory.getLog(JCLTest.class); BasicConfigurator.configure(); log.fatal("s"); log.error("error"); log.warn("warning"); log.info("info"); log.debug("debug"); log.trace("trace"); }
|
JCL原理
1 2 3 4 5 6
| private static final String[] classesToDiscover = new String[{ "org.apache.commons.logging.impl.Log4JLogger", "org.apache.commons.logging.impl.Jdk14Logger", "org.apache.commons.logging.impl.Jdk13LumberjackLogger", "org.apache.commons.logging.impl.SimpleLog" };
|
流程如下:
org.apache.commons.logging.LogFactory#getLog(java.lang.Class)
org.apache.commons.logging.impl.LogFactoryImpl#getInstance(java.lang.String)
org.apache.commons.logging.impl.LogFactoryImpl#newInstance
org.apache.commons.logging.impl.LogFactoryImpl#discoverLogImplementation
1 2 3
| for(int i = 0; i < classesToDiscover.length && result == null; ++i) { result = this.createLogFromClass(classesToDiscover[i], logCategory, true); }
|