Tomcat的多webapp要分日志打印的話,簡單方案是把日志JAR包各放各webapp的lib目錄,分別使用。但是這樣的話,日志JAR包加載多份,資源有所浪費。
最好是日志JAR包共用,用其它的隔離機制實現。
萬幸,log4j提供了一個叫LoggerRepository的API,可以做這個事情。
實現類圖:
本例以使用slf4j打印為例說明。
其中org.apache.log4j.Logger和org.apache.log4j.LoggerRepository為log4j包中類。
org.slf4j.Logger為slf4j-api中類。

Log4jLoggerAdapter用于將log4j的Logger轉換為slf4j的Logger,此類在slf4j包中已有,但其訪問權限有限,構造函數無修飾符。需要注意。
LogService代碼示意:
public?class?LogService {????private?org.apache.log4j.Logger?logger;????private?LoggerRepository?loggerRepository;????/**?????*?Instantiates?a?new?Oss?log?service.?????*?????*?@param?properties?the?properties?????*/????public?LogService(Properties?properties) ????{ ????????initEvn();????????new?PropertyConfigurator().doConfigure(properties,?loggerRepository); ????}????/**?????*?Instantiates?a?new?Oss?log?service.?????*?????*?@param?logFile?the?log?file?????*/????public?LogService(URL?logFile) ????{ ????????initEvn();????????new?PropertyConfigurator().doConfigure(logFile,?loggerRepository); ????}????private?void?initEvn() ????{????????logger?=?new?RootLogger(Level.ALL);????????loggerRepository?=?new?Hierarchy(logger); ????}????/**?????*?Gets?logger.?????*?????*?@param?name?the?name?????*?@return?the?logger?????*/????public?org.apache.log4j.Logger?getLogger(String?name) ????{????????return?loggerRepository.getLogger(name); ????} }
MyLogFactory示例:
public?final?class?MyLogFactory {????private?static?final?MyLogFactory?FACTORY?=?new?MyLogFactory();????private?final?Map>?instances?=?new?ConcurrentHashMap>();????private?final?static?Map?SERVICES?=?new?ConcurrentHashMap();????private?static?final?Map?APP_NAMES?=?new?ConcurrentHashMap();????/**?????*?Do?configuration.?????*?????*?@param?propertiesFile?the?properties?file?????*/????public?static?void?doConfiguration(File?propertiesFile) ????{????????if?(propertiesFile?==?null) ????????{????????????return; ????????} ????????String?serviceName?=?getAppName(); ????????LogService?logService?=?SERVICES.get(serviceName);????????if?(logService?==?null) ????????{????????????try????????????{ ????????????????logService?=?new?LogService(propertiesFile.toURI().toURL());????????????????SERVICES.put(serviceName,?logService); ????????????}????????????catch?(MalformedURLException?e) ????????????{ ????????????????logService?=?new?LogService(new?Properties());????????????????SERVICES.put(serviceName,?logService); ????????????} ????????} ????}????/**?????*?Do?configuration.?????*?????*?@param?properties?the?properties?????*/????public?static?void?doConfiguration(Properties?properties) ????{????????if?(properties?==?null) ????????{????????????return; ????????} ????????String?serviceName?=?getAppName(); ????????LogService?logService?=?SERVICES.get(serviceName);????????if?(logService?==?null) ????????{ ????????????logService?=?new?LogService(properties);????????????SERVICES.put(serviceName,?logService); ????????} ????}????/**?????*?Gets?log.?????*?????*?@param?arg0?the?arg?0?????*?@return?the?log?????*/????public?static?Logger?getLog(Class>?arg0) ????{????????return?FACTORY.getInstance(arg0.getName()); ????}????/**?????*?Gets?logger.?????*?????*?@param?arg0?the?arg?0?????*?@return?the?logger?????*/????public?static?Logger?getLogger(Class>?arg0) ????{????????return?FACTORY.getInstance(arg0.getName()); ????}????/**?????*?Gets?log.?????*?????*?@param?name?the?name?????*?@return?the?log?????*/????public?static?Logger?getLog(String?name) ????{????????return?FACTORY.getInstance(name); ????}????/**?????*?Gets?security?log.?????*?????*?@return?the?security?log?????*/????public?static?Logger?getSecurityLog() ????{????????return?FACTORY.getInstance("securityLogger"); ????}????/**?????*?Gets?instance.?????*?????*?@param?name?the?name?????*?@return?the?instance?????*/????public?Logger?getInstance(String?name) ????{ ????????String?serviceName?=?getAppName(); ????????Map?logMap?=?instances.get(serviceName);????????if?(logMap?==?null) ????????{ ????????????logMap?=?new?ConcurrentHashMap();????????????instances.put(serviceName,?logMap); ????????????Logger?log?=?getLogByName(serviceName,?name); ????????????logMap.put(name,?log);????????????return?log; ????????} ????????Logger?log?=?logMap.get(name);????????if?(log?==?null) ????????{ ????????????log?=?getLogByName(serviceName,?name); ????????????logMap.put(name,?log);????????????return?log; ????????}????????return?log; ????}????private?Logger?getLogByName(String?serviceName,?String?name) ????{ ????????LogService?logService?=?SERVICES.get(serviceName);????????if?(null?==?logService) ????????{????????????try????????????{ ????????????????File?appRootDir?=?new?File(getAppRoot(),?"etc"); ????????????????File?logProperties?=?new?File(appRootDir,?"log4j/log4j-app.properties"); ????????????????logService?=?new?LogService(logProperties.toURI().toURL());????????????????SERVICES.put(serviceName,?logService); ????????????}????????????catch?(MalformedURLException?e) ????????????{ ????????????????logService?=?new?LogService(new?Properties());????????????????SERVICES.put(serviceName,?logService); ????????????} ????????} ????????org.apache.log4j.Logger?log4jLogger?=?logService.getLogger(name);????????return?new?Log4jLoggerAdapter(log4jLogger); ????}????public?static?String?getLogHome() ????{ ????????...... ????}????private?static?String?getProcessName() ????{ ????????......????????return?......; ????}????/**?????*?get?app?name?????*?@return?the?app?name?????*/????public?static?String?getAppName() ????{ ????????String?clazzPath?=?"main"; ????????URL?resourceUrl?=?Thread.currentThread().getContextClassLoader().getResource("/");????????if?(null?!=?resourceUrl) ????????{ ????????????clazzPath?=?resourceUrl.getPath(); ????????}????????if?(!APP_NAMES.containsKey(clazzPath)) ????????{ ????????????String?appName?=?clazzPath.split("-")[1].split("/")[0];????????????APP_NAMES.put(clazzPath,?appName); ????????}????????return?APP_NAMES.get(clazzPath); ????}????private?static?String?getAppRoot() ????{????????return?...?... ????} }
使用示例:
使用前,調用MyLogFactory.doConfiguration()將log4j.properties配置文件配置到相應webapp的logRepository中。
然后在業務類中使用Logger log = MyLogFactory.getLogger(A.class)獲取日志對象,就可以正常使用了。
JAR Tomcat
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
下一篇:找出Word2013文檔存儲路徑的兩種方法(word文檔保存路徑)