比如说记日志,日志模块怎么知道它被谁调用了???
抛异常就别说了,效率相对来说低了一点。有没有高明一点的办法?

解决方案 »

  1.   

    举个例看……
    不甚明白你们的意思……
    我不需要用户名和时间之类的。譬如说com.exam.Example调用了log,
    希望log记录下“com.exam.Example"
      

  2.   

    http://www.donews.net/xzwenlan/archive/2005/02/20/282565.aspx
    楼主您有空到这里看看
    对您可能有帮助
      

  3.   

    那上面有关于堆栈的简介,可能不是楼主要问的
    对不起楼主啦
    爱莫能助
    学习ing
    帮您顶
      

  4.   

    就好像Logger.getLogger("com.aa.bb");//当前日志类
    用Exception也是一个办法。。
    看看log4j或者有吧
      

  5.   

    譬如说com.exam.Example调用了log,
    希望log记录下“com.exam.Example"////////////////////////////////////你用Class类的方法不就可以得到哪个class调到log了?,
    然后Logger.getLogger(""),放到里面不就ok了?
      

  6.   

    问题想复杂了吧
    塞个OB在被监视方法中即可
    或者你创造一个被监听方法,该方法继承或者运行你要的方法
    这样做更OO.
      

  7.   

    找<java virtual machine>看看
    这里有关堆和栈的相关介绍
    感觉还算详细啦
      

  8.   

    你用Class类的方法不就可以得到哪个class调到log了……
    具体是哪个方法啊,我找了半天没找到……
    不过eclipse里确实都可以看到那个class的信息了,只是我
    找不到具体的方法而已……晕。
    塞个OB在被监视方法中即可
    或者你创造一个被监听方法,该方法继承或者运行你要的方法
    你的意思是??我怎么不太明白?被监听的方法?
      

  9.   

    有一个sun的内部使用的类里面有一个方法可以得到调用者的类, 不过我觉得还是要小心一点儿,尽量不要使用这种内部的类sun.reflect.Reflection 类    /** Returns the class of the method <code>realFramesToSkip</code>
            frames up the stack (zero-based), ignoring frames associated
            with java.lang.reflect.Method.invoke() and its implementation.
            The first frame is that associated with this method, so
            <code>getCallerClass(0)</code> returns the Class object for
            sun.reflect.Reflection. Frames associated with
            java.lang.reflect.Method.invoke() and its implementation are
            completely ignored and do not count toward the number of "real"
            frames skipped. */
        public static native Class getCallerClass(int realFramesToSkip);