国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Log信息獲取調(diào)用類和調(diào)用方法名的實(shí)現(xiàn)原理

2019-11-18 12:03:10
字體:
供稿:網(wǎng)友

  Sun JDK 源代碼下載 http://wwws.sun.com/software/communitysource/
  先注冊(cè)并登錄到“Sun Community Source Licensing”,然后下載J2SE(幾十兆)或者J2EE(幾百兆)。
  
  Log能夠把代碼運(yùn)行時(shí)間,類名,方法名,還有信息,全部都打印出來。
  一個(gè)直觀的例子,每次啟動(dòng)Tomcat(缺省配置)的時(shí)候。一般可以看到
  Jul 9, 2004 11:22:29 AM org.apache.struts.util.PRopertyMessageResources
  INFO: Initializing, config='org.apache.webapp.admin.applicationResources', returnNull=true
  Jul 9, 2004 11:22:41 AM org.apache.coyote.http11.Http11Protocol start
  INFO: Starting Coyote HTTP/1.1 on port 8080
  
  時(shí)間,類名,方法名,信息都打印了出來。
  那么,log是如何獲取調(diào)用自身的這個(gè)類和這個(gè)方法名的呢?
  
  后面給出的代碼是JDK1.4的源代碼,和Log4J的源代碼。說明其實(shí)現(xiàn)原理。
  獲得調(diào)用類,和方法名,就是需要獲得當(dāng)前運(yùn)行棧的結(jié)構(gòu)。
  new Throwable().getStackTrace() 會(huì)返回當(dāng)前運(yùn)行棧的結(jié)構(gòu)層次。
  利用這種原理,可以獲得整個(gè)運(yùn)行棧的調(diào)用關(guān)系。
  
  JDK1.4的java.util.logging包, 通過Throwable.getStackTrace()方法實(shí)現(xiàn)的。
  // Get the stack trace.
  StackTraceElement stack[] = (new Throwable()).getStackTrace();
  
  完整的代碼在JDK1.4的源代碼里面,java.util.logging.LogRecord類的inferCaller方法。
  
  java代碼:
  2 // Private method to infer the caller's class and method names
  3 private void inferCaller() {
  ...}
  4 needToInferCaller = false;
  5 // Get the stack trace.
  6 StackTraceElement stack[] = (new Throwable()).getStackTrace();
  7 // First, search back to a method in the Logger class.
  8 int ix = 0;
  9 while (ix < stack.length) {
  ...}
  10 StackTraceElement frame = stack[ix];
  11 String cname = frame.getClassName();
  12 if (cname.equals("java.util.logging.Logger")) {
  ...}
  13 break;
  14 }
  15 ix++;
  16 }
  17 // Now search for the first frame before the "Logger" class.
  18 while (ix < stack.length) {
  ...}
  19 StackTraceElement frame = stack[ix];
  20 String cname = frame.getClassName();
  21 if (!cname.equals("java.util.logging.Logger")) {
  ...}
  22 // We've found the relevant frame.
  23 setSourceClassName(cname);
  24 setSourceMethodName(frame.getMethodName());
  25 return;
  26 }
  27 ix++;
  28 }
  29 // We haven't found a suitable frame, so just punt. This is
  30 // OK as we are only commited to making a "best effort" here.
  31 }
  32
  
  Log4j有異曲同工之妙。
  org.apache.log4j.spi.LocationInfo類。
  先用Throwable.printStackTrace()方法把Exception信息打印到一個(gè)字符串里。
  然后按行分析這個(gè)字符串。抽出調(diào)用類和方法。參見LocationInfo類的構(gòu)造函數(shù)。
  
  java代碼:
  1
  2 /**
  3 Instantiate location information based on a Throwable. We
  4 eXPect the Throwable t, to be in the format
  5
  6 

  7 java.lang.Throwable
  8 ...
  9 at org.apache.log4j.PatternLayout.format(PatternLayout.java:413)
  10 at org.apache.log4j.FileAppender.doAppend(FileAppender.java:183)
  11 at org.apache.log4j.Category.callAppenders(Category.java:131)
  12 at org.apache.log4j.Category.log(Category.java:512)
  13 at callers.fully.qualified.className.methodName(FileName.java:74)
  14 ...
  15

  16
  17



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 鄄城县| 金湖县| 桐乡市| 余干县| 米泉市| 永胜县| 油尖旺区| 高雄县| 霍山县| 嘉义市| 云浮市| 平安县| 阳高县| 农安县| 仙居县| 平阴县| 富川| 揭西县| 浠水县| 舞钢市| 鄂伦春自治旗| 尉氏县| 青阳县| 托里县| 宣武区| 红安县| 乐山市| 金门县| 彭水| 阿瓦提县| 大冶市| 牟定县| 剑阁县| 灵山县| 长宁区| 页游| 邵东县| 双流县| 屏南县| 嘉禾县| 乐业县|