我怎么知道哪个线程称为我的Log方法?

Kau*_*kar 4 java logging multithreading

我有一个Log类,它有几个静态方法,可以帮助记录有关我的程序的信息.

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

我想让我的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 );
     }
 }
Run Code Online (Sandbox Code Playgroud)

dbf*_*dbf 8

将此添加到记录器:

Thread t = Thread.currentThread();
String name = t.getName();
Run Code Online (Sandbox Code Playgroud)

并将其转储到日志文件中.