2011-12-30 71 views
4

我有一个日志类,有几个静态方法,帮助记录有关我的程序的信息。如何知道哪个线程称为我的日志方法?

我的问题是我有2个线程正在运行,并且他们都向我的Log类发送请求以记录信息。

我想让我的日志类显示哪些线程正在记录哪些线。

我应该怎么做才能实现这个功能?

我的代码基本上是这样的:

public class Log { 
    public static void log (String tag , Object message) 
    { 
     String lineToPrint = ""; 
     //Builds the string taking in time data and other information 
     //... 
     //This is where I want to see which thread called this log function 
     //... 

     System.out.println(lineToPrint); 
    } 
} 
+2

你为什么不使用现有的日志框架之一,如log4j(可能使用apache commons logging进行包装)? – Thomas 2011-12-30 07:31:11

回答

8

添加到您的记录:

Thread t = Thread.currentThread(); 
String name = t.getName(); 

和转储到日志文件。

+0

太棒了!感谢dbf! – 2011-12-30 07:42:55

0
public class Temp{ 
    static Thread t = null; 
} 

temp.t = Thread.currentThread();

public static void log (String tag , Object message) 
{ 
     temp.t.getName();//get it 
} 
相关问题