服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

log信息获取调用类和调用方法名的实现原理


  sun jdk 源代码下载 http://wwws.sun.com/software/communitysource/
  先注册并登录到“sun community source licensing”,然后下载j2se(几十兆)或者j2ee(几百兆)。
  
  log能够把代码运行时间,类名,方法名,还有信息,全部都打印出来。
  一个直观的例子,每次启动tomcat(缺省配置)的时候。一般可以看到
  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
  
  时间,类名,方法名,信息都打印了出来。
  那么,log是如何获取调用自身的这个类和这个方法名的呢?
  
  后面给出的代码是jdk1.4的源代码,和log4j的源代码。说明其实现原理。
  获得调用类,和方法名,就是需要获得当前运行栈的结构。
  new throwable().getstacktrace() 会返回当前运行栈的结构层次。
  利用这种原理,可以获得整个运行栈的调用关系。
  
  jdk1.4的java.util.logging包, 通过throwable.getstacktrace()方法实现的。
  // 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信息打印到一个字符串里。
  然后按行分析这个字符串。抽出调用类和方法。参见locationinfo类的构造函数。
  
  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

however, we can also deal with jit compilers that "lose" the
  18 location information, especially between the parentheses.
  19
  20 */
  21 public locationinfo(throwable t, string fqnofcallingclass)
  22
  
  e.printstacktrace()把exception发生当时的整个运行栈结构展开,打印出来。
  log4j就是分析这个打印结果,获得所有的调用层次。
  
  关于直接获取调用类名的方法。
  我们来看sun.reflect.reflection的getcallerclass()方法的说明。
  
  java代码:
  1
  2 /** returns the class of the method realframestoskip
  3 frames up the stack (zero-based), ignoring frames associated
  4 with java.lang.reflect.method.invoke() and its implementation.
  5 the first frame is that associated with this method, so
  6 getcallerclass(0) returns the class object for
  7 sun.reflect.reflection. frames associated with
  8 java.lang.reflect.method.invoke() and its implementation are
  9 completely ignored and do not count toward the number of "real"
  10 frames skipped. */
  11 public static native class getcallerclass(int realframestoskip);
  
  这是一个native方法。原理也是根据stackframe(运行栈)获取相应类的信息。这个方法直接返回一个class名字,直接有效。参数realframestoskip用来选取你所需要的stack层次,所以,你可以用这个方法获得任何层次的上的调用类名。
  
  throwable.getstacktrace()也是一个native方法。原理也是根据stackframe(运行栈)获取相应类的信息。返回一个stacktraceelement[]。
  stacktraceelement类在jdk1.4的java.lang的包里面。里面包含丰富的信息,非常适合debug。
  stacktraceelement类有如下方法:
  getfilename(),getlinenumber(), getclassname(), getmethodname()。

扫描关注微信公众号