• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to ues log4j to send log email in the multithreaded processes as similiar with single-threaded  RSS feed

 
Sky Loi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My current system is single-threaded with several modules which are processed in sequence. I use SMTPAppender to capture error and send warning emails with the following format:
[time][thread-1][module 1]start: XXXXXXX
[time][thread-1][module 2]XXXXXXX
[time][thread-1][module 3]XXXXXXX
[time][thread-1][module 4]ERR: XXXXXXX


After I change the system to multhithreaded, the logs will be tangled up like:
[time][thread-1][module 1]start: XXXXXXX
[time][thread-2][module 1]start: XXXXXXX
[time][thread-1][module 2]XXXXXXX
[time][thread-1][module 3]XXXXXXX
[time][thread-2][module 2]XXXXXXX
[time][thread-2][module 3]XXXXXXX
[time][thread-2][module 4]end: XXXXXXX
[time][thread-1][module 4]ERR: XXXXXXX
.....

Moreover, if the buffer size in SMTPAppender is set as small, some reference info about thread-1 will be filtered. Is there any way to format the log email in the multhithreaded processes as similiar with single-threaded? Customize the SMTPAppender?
 
Paul Clapham
Sheriff
Posts: 22505
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need one SMTPAppender per thread if that's the way you want to see the output.
 
Sky Loi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, thank you for your quick advice, Paul.

Do you mean I need to create Logger per thread as well? So far, my Logger is implemented as singleton and shared by threads. The following is my assumption:

Module 1: new Logger and new SMTPAppender as non-singleton; pass the Logger reference to Module 2 ....
Module 2: use the same Logger; pass the Logger reference to Module 3 ....
....


 
Sky Loi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dose it has performance issue if creating new logger instance per thread?

And is there any suggestion how to save the logger instance to be shared by modules? I want to use a concurrent hashmap to store the logger by thread ID. Is it good?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!