Can two log4j filesappenders be written to one file?

For a second, forget the question of why you should do this - if for some reason two FileAppenders files are configured with the same file - will this setting work?

+4
source share
3 answers

Log4j FileAppender cannot write two JVMs to the same file. If you try, you will get a damaged log file. However, logback, log4j successor, in reasonable mode allows two applications to write to the same file even in different JVMs.

+3
source

It does not directly answer your question, but log4 * net * FileAppender has the LockingModel attribute, which can only be set to lock when the file is actually used. Therefore, if you had two FileAppenders working on the same thread as the MinimalLock, it would probably work just fine. In different threads, you can sometimes get stuck.

FileAppender supports plug-in file-locking models using the LockingModel property. The default behavior implemented by FileAppender.ExclusiveLock is to obtain an exclusive write lock on the file until this appender is closed. The alternative FileAppender.MinimalLock model contains only a write lock, while the application records a registration event.

A quick web search did not reveal any useful results about using MinimalLock in log4j.

0
source

From Log4j FAQ a3.3

How do I get multiple processes to register in one file?

You can have every process log on SocketAppender. The receiving SocketServer (or SimpleSocketServer) can receive all events and send them to a single log file.

As for what it really means that I will explore myself.

I also found the following workaround for another fooobar.com/questions/648696 / ... question:

Code + Example

0
source

All Articles