This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

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

 
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?
 
Sheriff
Posts: 24654
58
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?
 
Whatever. Here's a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!