I have used the log4cxx in my project for logging purpose. But, recently I noticed that some two different log lines are mixed with each other. I am not sure what was the problem. For example, I am expecting the log lines like below,
Printing the test line one
Printing the test line two
Printing the test line three
But, the log is written like below,
Printing the test Printing the test line two
line one
Printing the test line one
Note: I have used the multi-thread and multi-process with my application.
It would be great if anyone provides the right solution for this one.
Thanks in advance.
I already tried with setBufferSize and setImmediateFlush option with log4cxx. But, not working.
I have used the multi-thread and multi-process with my application.
And that's the issue. Apparently log4cxx is not thread-safe, at least not the facilities you are currently using. What then happens is that one thread starts writing a log message, gets interrupted by another thread, which again starts writing a message, placing it right within the already started, but not finished one of the first thread.
You could solve the issue e. g. by having separate log files for every thread. Not sure how far supported by log4cxx natively (I'm not too familiar with it), something like thread-local FileAppender
might do the trick, though.
Otherwise, you'll need to somehow protect the point log4cxx is writing to a file against race conditions. Please evaluate NDC first, at a first glance, that might fit your needs, though I didn't dig deeper... If not supported natively you might wrap the non-thread-safe FileAppender
with some custom, thread-safe Appender class of your own.
If you want to write to one single file from multiple processes, you need to synchronise across different processes as well. Support for relies entirely on OS, not all give you appropriate support for, though.
If logging to database is an option, ODBCFileAppender
appears interesting as well. I would assume (but you'd need to verify yourself) that every log message results in its own SQL query, thus write synchronisation would be left to the DBMS. That would work not only for threads, but for processes as well.
Finally: You could write your own logging server, accepting connections (e. g. pipes or sockets) from different threads. It would buffer incoming log messages for every connection and write them one after another to file. That would be safe across different threads as well, similar as the database solution.