Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know which Thread called my Log method?

I have a Log class that has several static methods which help log information about my program.

My problem is that I have 2 threads running and both of them send requests to my Log class to log information.

I would like to have my Log class show which threads are logging which lines.

What should I do to achieve this functionality?

My code is basically like this:

 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 );
     }
 }
like image 551
Kaushik Shankar Avatar asked Feb 23 '23 00:02

Kaushik Shankar


1 Answers

Add this to your logger:

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

and dump it to log file.

like image 60
dbf Avatar answered Feb 24 '23 14:02

dbf