今天进行slf4j中logger的同步封装。主要目的是为了以后方便更换日志实现系统。

遇到的问题:使用Thread.currentThread().getStackTrace()[1].getClassName()得到的是当前类而不是调用类。见以下代码:

private org.slf4j.Logger logger = null;

	/**
* construction method
*/
public Logger(){
// get the current class logger
logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[1].getClassName());
}

所以打印日志的时候并没有得到日志真正所属类的信息。

然后,我进行了一组測试:

測试A:

public class ThreadTest {

	public static void TestString(){
StackTraceElement[] arr = new Exception().getStackTrace();
for(int i=0;i<=arr.length-1;i++){
System.out.println(arr[i].getClassName()+";"+arr[i].getMethodName()+";"+arr[i].getFileName());
}
}
}
public class App
{
public static void main( String[] args )
{
ThreadTest.TestString();
}
}

结果是:

test.ThreadTest;TestString;ThreadTest.java(当前方法和类)

test.App;main;App.java(调用该方法的方法和类)

測试B:

public class ThreadTest {

	public static void TestString(){
StackTraceElement[] arr = Thread.currentThread().getStackTrace();
for(int i=0;i<=arr.length-1;i++){
System.out.println(arr[i].getClassName()+";"+arr[i].getMethodName()+";"+arr[i].getFileName());
}
}
}

App类同上。得到的结果是:

java.lang.Thread;getStackTrace;Thread.java(Thread的信息)

test.ThreadTest;TestString;ThreadTest.java(当前方法和类)

test.App;main;App.java(调用该方法的方法和类)

啥米情况???难道就是由于Thread的getStackTrace比Throwable(Exception的父类)的getStackTrace多打了一段Thread类的信息???

因为不确定是不是真的是这样子的,故去查看了下jdk的源代码(纯属装X,不抱幻想)。

首先是Throwable的getStackTrace方法,代码例如以下:

 public StackTraceElement[] getStackTrace() {
return getOurStackTrace().clone();
} private synchronized StackTraceElement[] getOurStackTrace() {
// Initialize stack trace field with information from
// backtrace if this is the first call to this method
if (stackTrace == UNASSIGNED_STACK ||
(stackTrace == null && backtrace != null) /* Out of protocol state */) {
int depth = getStackTraceDepth();
stackTrace = new StackTraceElement[depth];
for (int i=0; i < depth; i++)
stackTrace[i] = getStackTraceElement(i);
} else if (stackTrace == null) {
return UNASSIGNED_STACK;
}
return stackTrace;
}

貌似没有什么不正确劲的地方哈,当中

int depth = getStackTraceDepth();
stackTrace[i] = getStackTraceElement(i);

两个方法都是native的,不予深究了。这里没有找到问题。就去看了下Thread的getStackTrace方法,代码例如以下:

public StackTraceElement[] getStackTrace() {
if (this != Thread.currentThread()) {
// check for getStackTrace permission
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(
SecurityConstants.GET_STACK_TRACE_PERMISSION);
}
// optimization so we do not call into the vm for threads that
// have not yet started or have terminated
if (!isAlive()) {
return EMPTY_STACK_TRACE;
}
StackTraceElement[][] stackTraceArray = dumpThreads(new Thread[] {this});
StackTraceElement[] stackTrace = stackTraceArray[0];
// a thread that was alive during the previous isAlive call may have
// since terminated, therefore not having a stacktrace.
if (stackTrace == null) {
stackTrace = EMPTY_STACK_TRACE;
}
return stackTrace;
} else {
// Don't need JVM help for current thread
return (new Exception()).getStackTrace();
}
}

乍一看也没啥错啊~~~不正确,突然间跟着逻辑走一下。在if语句里面由于

this != Thread.currentThread()是为true的。所以方法会运行else里面的语句,里面是啥。!!

return (new Exception()).getStackTrace();

All right!

就是这里,这里jvm去运行了new Exception().getStackTrace();就是这句话让使用Thread的getStackTrace方法就有可能多打印一句java.lang.Thread;getStackTrace;Thread.java出来。这也就是为什么使用

logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[1].getClassName());

会得不到正确的日志信息的原因了。

应该就是这样子了。为了验证我想的对不正确,写了一个測试验证的样例,代码例如以下:

public class TestException {

	public static StackTraceElement[] getStackTrace() {
return new Exception().getStackTrace();
}
}
public class ThreadTest {

	public static void TestString(){
StackTraceElement[] arr = TestException.getStackTrace();
for(int i=0;i<=arr.length-1;i++){
System.out.println(arr[i].getClassName()+";"+arr[i].getMethodName()+";"+arr[i].getFileName());
}
}
}
public class App
{
public static void main( String[] args )
{
ThreadTest.TestString();
}
}

运行之后的结果是:

test.TestException;getStackTrace;TestException.java

test.ThreadTest;TestString;ThreadTest.java

test.App;main;App.java

红色的一行即证明了我的想法是正确的!

全部的验证至此结束了。这个问题最后解决的方法非常easy,要么使用new Exception().getStackTrace()[1];要么使用Thread.currentThread().getStackTrace()[2]。

尽管这个问题非常小,也非常基础。可是我还是非常兴奋。毕竟粘上了源代码,顿时逼格升高不少~~~

新手上路。高手饶命~!

得到当前堆栈信息的两种方式(Thread和Throwable)的纠结的更多相关文章

  1. 26.OpenIdConnect获取用户信息的两种方式

    openId在OAuth基础之上,在下面这红框内拿到Authorization Code之后还可以返回IdToken. IdToken和AccessToken一起返回.IdToken就会包括了用户的信 ...

  2. 程序中使用log4J打印信息的两种方式

    (1)通过org.apache.commons.logging.Log 接口实例化: public static Log log = LogFactory.getLog(String name); p ...

  3. 在Scrapy中如何利用Xpath选择器从HTML中提取目标信息(两种方式)

    前一阵子我们介绍了如何启动Scrapy项目以及关于Scrapy爬虫的一些小技巧介绍,没来得及上车的小伙伴可以戳这些文章: 手把手教你如何新建scrapy爬虫框架的第一个项目(上) 手把手教你如何新建s ...

  4. 读取数据库配置信息的两种方式(以后开发项目用java链接数据库)-------java基础知识

    第一步:先建立jdbc.properties user=root password url/yanlong driver=com.mysql.jdbc.Driver 第一种方式:直接文件读取 pack ...

  5. 删除SVN版本信息的两种方式

    一.在linux下删除SVN版本信息 删除这些目录是很简单的,命令如下 find . -type d -name ".svn"|xargs rm -rf 或者 find . -ty ...

  6. django--通过jwt获取用户信息的两种方式

    HTTP请求是无状态的,我们通常会使用cookie或session对其进行状态保持,cookie存储在客户端,容易被用户误删,安全性不高,session存储在服务端,在服务器集群情况下需要解决sess ...

  7. linux内核分析作业4:使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用

    系统调用:库函数封装了系统调用,通过库函数和系统调用打交道 用户态:低级别执行状态,代码的掌控范围会受到限制. 内核态:高执行级别,代码可移植性特权指令,访问任意物理地址 为什么划分级别:如果全部特权 ...

  8. Struts2实现ajax的两种方式

    基于Struts2框架下实现Ajax有两种方式,第一种是原声的方式,另外一种是struts2自带的一个插件. js部分调用方式是一样的: JS代码: function testAjax() { var ...

  9. 在基于MVC的Web项目中使用Web API和直接连接两种方式混合式接入

    在我之前介绍的混合式开发框架中,其界面是基于Winform的实现方式,后台使用Web API.WCF服务以及直接连接数据库的几种方式混合式接入,在Web项目中我们也可以采用这种方式实现混合式的接入方式 ...

随机推荐

  1. ASP.NET 弹出对话框和页面之间传递值的经验总结

    今天碰到一个弹出对话框(PopUp dialog)的问题, 因该是个傻瓜问题, 但是还是让我研究了半天, 总结了一些前人经验, 拿出来跟大家分享一下! 在ASP.Net中页面之间的传值方法有很多,但是 ...

  2. javaweb学习总结(三十九)——数据库连接池

    一.应用程序直接获取数据库连接的缺点 用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长.假设网站一天10万访问量,数据库服务器就需要创建10万次连接,极大 ...

  3. 转:HTTP请求(GET、POST和soap区别)和响应

    一直对Http请求和SOAP请求不是太理解,只是知道SOAP是基于Http的,并且增加了很多XML标签,SOAP经常用在WebService中,比如在C#中创建一个WebService,然后在客户端生 ...

  4. 1uboot移植要点[原创☆☆]

    ----- 一:我们先来了解下实际内存: nand.nor.ram. 所以从CPU是从那部分启动的呢? 答:要看主控芯片的boot如何设置(正如分的启动方式和下载方式一样). uboot:sd卡→iR ...

  5. Linux 线程优先级

    http://www.cnblogs.com/imapla/p/4234258.html http://blog.csdn.net/lanseshenhua/article/details/55247 ...

  6. ActionBar官方教程(9)ActionBar的顶部tab模式(注意,已经被弃用)

    This interface is deprecated.Action bar navigation modes are deprecated and not supported by inline ...

  7. MCI音乐播放

    缘由: 在改正俄罗斯方块程序的功能的时候,想给这个程序增加一个背景音乐.本想用PlayWave来做的,但想到这个功能十分常用,那还不如封装一个自己的CMusic 类,以备不时之需.本来以为很容易的,可 ...

  8. 兼容ie\firefox\chrome的cursor

    cursor:hand 与 cursor:pointer 的效果是一样,都像手形光标. 但用FireFox浏览时才注意到使用cursor:hand在FireFox.chorme里并被支持.cursor ...

  9. [039] 微信公众帐号开发教程第15篇-自定义菜单的view类型(访问网页)

    引言及内容概要 距离写上一篇文章<自定义菜单的创建及菜单事件响应>整整过了两个月的时间,那时公众平台还没有开放view类型的菜单.在不久前,微信公众平台悄悄开放了view类型的菜单,却没有 ...

  10. c#打印机设置,取得打印机列表及相应打印机的所有纸张格式

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...