武漢市二手房交易合同備案在那個(gè)網(wǎng)站上做呀如何讓網(wǎng)站快速收錄
我們?cè)谑褂?Spring5 的過(guò)程中會(huì)出現(xiàn)這樣的現(xiàn)像,就是 Spring5 內(nèi)部代碼打印的日志和我們自己的業(yè)務(wù)代碼打印日志使用的不是統(tǒng)一日志實(shí)現(xiàn),尤其是在項(xiàng)目啟動(dòng)的時(shí)候,Spring5 的內(nèi)部日志使用的是 log4j2,但是業(yè)務(wù)代碼打印使用的可能是 logback ,log4j1 或者 其他日志實(shí)現(xiàn)
一、Spring4 采用日志是 commons-logging
二、Spring5 內(nèi)部日志
1.Spring5 內(nèi)部日志原理
Spring5 自己在?commons-logging 基礎(chǔ)上進(jìn)行修改,將修改完的代碼單獨(dú)封裝為一個(gè) jar 包,Spring5 內(nèi)部就使用這個(gè)jar,我們可以注意一下當(dāng)我們導(dǎo)入 Spring5 的相關(guān) jar 包的時(shí)候,maven會(huì)為我們自動(dòng)依賴一個(gè) spring-jcl 的jar包,這就是那個(gè)jar包。
?
?
?可以看出這這兩 jar 包的包名都起的一模一樣,只是LogFactory的實(shí)現(xiàn)是Spring5自己實(shí)現(xiàn)的。
2. Spring5 日志關(guān)鍵源碼分析
org.apache.commons.logging.LogFactory#getLog(java.lang.String)
/*** Convenience method to return a named logger.* @param name logical name of the <code>Log</code> instance to be returned*/public static Log getLog(String name) {return LogAdapter.createLog(name);}
org.apache.commons.logging.LogAdapter#createLog
private static final String LOG4J_SPI = "org.apache.logging.log4j.spi.ExtendedLogger";private static final String LOG4J_SLF4J_PROVIDER = "org.apache.logging.slf4j.SLF4JProvider";private static final String SLF4J_SPI = "org.slf4j.spi.LocationAwareLogger";private static final String SLF4J_API = "org.slf4j.Logger";private static final LogApi logApi;static {
//LOG4J_SPI = "org.apache.logging.log4j.spi.ExtendedLogger"在Log4j2包log4j-api中
//LOG4J_SLF4J_PROVIDER =
// "org.apache.logging.slf4j.SLF4JProvider"在Log4j2橋接器包log4j-to-slf4j中
//SLF4J_SPI = "org.slf4j.spi.LocationAwareLogger" 在slf4j包 slf4j-api 中
//SLF4J_API = "org.slf4j.Logger" 在slf4j包 slf4j-api 中//判斷是否導(dǎo)入了 log4j2包 log4j-apiif (isPresent(LOG4J_SPI)) {if (//判斷是否導(dǎo)入了 Log4j2 橋接器包log4j-to-slf4jisPresent(LOG4J_SLF4J_PROVIDER) &&//判斷是否導(dǎo)入了 slf4j包 slf4j-apiisPresent(SLF4J_SPI)) {// log4j-to-slf4j bridge -> we'll rather go with the SLF4J SPI;// however, we still prefer Log4j over the plain SLF4J API since// the latter does not have location awareness support.logApi = LogApi.SLF4J_LAL;}else {// Use Log4j 2.x directly, including location awareness supportlogApi = LogApi.LOG4J;}}else if (isPresent(SLF4J_SPI)) {// Full SLF4J SPI including location awareness supportlogApi = LogApi.SLF4J_LAL;}else if (isPresent(SLF4J_API)) {// Minimal SLF4J API without location awareness supportlogApi = LogApi.SLF4J;}else {// java.util.logging as defaultlogApi = LogApi.JUL;}}// LOG4J 采用 log4j2的 日志實(shí)現(xiàn)// SLF4J_LAL 采用 slf4j Slf4jLocationAwareLog Full SLF4J SPI
//including location awareness support// SLF4J 采用 slf4j Slf4jLog Minimal SLF4J API without
//location awareness supportprivate enum LogApi {LOG4J, SLF4J_LAL, SLF4J, JUL}public static Log createLog(String name) {switch (logApi) {case LOG4J:return Log4jAdapter.createLog(name);case SLF4J_LAL:return Slf4jAdapter.createLocationAwareLog(name);case SLF4J:return Slf4jAdapter.createLog(name);default:// Defensively use lazy-initializing adapter class here as well since the// java.logging module is not present by default on JDK 9. We are requiring// its presence if neither Log4j nor SLF4J is available; however, in the// case of Log4j or SLF4J, we are trying to prevent early initialization// of the JavaUtilLog adapter - e.g. by a JVM in debug mode - when eagerly// trying to parse the bytecode for all the cases of this switch clause.return JavaUtilAdapter.createLog(name);}
}