1

I have started to use log4cpp. I ran some tests on it because I want to have multiple processes all writing to the same files.

I know its thread safe, and I did some testing on inter-process writing, it seemed to work. But then I read this:

3.2. Is log4cpp thread-safe? The same instance of the log4cpp::Category object (a logger) can be used from different threads simultaneously without explicit synchronization. Concurrent access to the appenders will be prevented by the logger object itself. It will lock internal mutex each time when it comes to writing into appenders. So, it is safe, for example, to write from the multiple threads to the same logger which appends to the same file. Although, log4cpp is configured in such a way that two different loggers append to the same appender (it may be a file), then there will be no way for the logging framework to arrange proper addition and things can get mixed up. So this way of configuration is not recommended.

From here, and now I have a doubt about that...

Does anyone have any experience with this?

code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • 1
    A few moments ago I was playing arround with log4cpp, log4cxx, boost.log and a few others. If you aren't forced to use log4cpp I would not recomend it. boost.log does support multithreaded/interprocess backends called: [IPC message queue backend](https://www.boost.org/doc/libs/1_67_0/libs/log/doc/html/log/detailed/sink_backends.html#log.detailed.sink_backends.text_ipc_message_queue), has better documentation and it's possible to extend it to have other backends (like network to syslog). – user1810087 Oct 31 '18 at 13:13
  • @user1810087 thanks for that, I'll certainly consider it... we are a little way committed into log4cpp, and it would be a little bit of a pain to switch, but we have wrapped it into our own API so maybe its not so bad - I'll take a look. But do you know if log4cpp supports inter-process properly? – code_fodder Oct 31 '18 at 13:58
  • I highly doubt it. Looking into the code, it seems they use a (regular) mutex or a recursive mutex (as default). Both result in one of: 1) A C++11 mutex - does not supprt IPC, 2) a pthread mutex- [kinda support IPC](https://stackoverflow.com/questions/6477525/interprocess-mutex-with-pthreads) 3) windows cirtical section - does support IPC or 4) a poor mans mutex (selfimplemented) - Does not support IPC. Plus they seem to lock the files. – user1810087 Oct 31 '18 at 15:05
  • @user1810087 My testing suggested that it supports IPC (only in that I did not lose any data or get any mangled data lines)... so maybe the file locking is what synchronises it... probably good enough for what I need... but not very efficient I guess! - thanks for that update – code_fodder Oct 31 '18 at 19:49

0 Answers0