Search code examples
javamultithreadingspring-webfluxjava.util.concurrent

How to share common log file among multiple threads in Java


Can we share same log file among multiple threads reading and writing into it.

Is it a good idea to dump messages in Deque like concurrent collection first and then periodically dump data from deque to log file.

Please share Any example/ pointers for best performance.


Solution

  • Assuming by threads you are referring to applications / programs running either on the same machine or in a distributed environment and not multiple threads within the same application.

    Then ...

    • If you are using Log4J as your logging framework you could implement your logging through a SocketAppender (see Log4J appenders). Then your various applications could connect to a separate "logging" service you've written to consolidate all the logs into a single file.

    • Another alternative is to have all your applications register as producers on a specific queue (something like RabbitMQ), where logs lines are posted as messages. Then you can again have a separate consumer, which consumes the messages and writes them to your log.