How to log multiple threads in different log files?

前端 未结 6 665
南旧
南旧 2020-12-05 12:40

I have a JAVA class that starts various threads that have unique IDs. Each thread should log into a unique log file, named after the ID.log.

Because I only get the u

6条回答
  •  情歌与酒
    2020-12-05 12:58

    What about adding a static instance counter variable to your class. Then you would need a synchronized method which increases the counter for each object created and create the log file name from that value. Something like this:

    class yourClass {
    
      private static int cnt = 0;
    
      public yourClass(){
        ...
        initLogger();
      }
    
      private synchronized initLogger(){
         yourClass.cnt++;
         myJobid = yourClass.cnt;
    
         //include your logging code here
      }
    }
    

提交回复
热议问题