I want to have a diagnostic log that is produced by several tasks managing data. These tasks may be in multiple threads. Each task needs to write an element (possibly with s
I had a similar problem and implementation demands for special logs only. My solution was:
I took a blockinglinkedqueue with size of *2 of the app's traffic/min.
All threads put the object in the queue and finishes the job.
Separate Log-Writer thread taking head object from queue and writing it to log4j file using a separate appender. This appender was not used for systemlogs.
This ensures that logs are written serially and always are in order.
This will not affect performance of the application since log writing is a completely separate process and will not create a bottleneck.
You can also use aysncappender of log4j.