5

Forget for a second the question of why on earth would you do such a thing - if, for whatever reason, two FileAppenders are configured with the same file - will this setup work?

0

3 Answers 3

4

Log4j's FileAppender does not allow for two JVM's writing to the same file. If you try, you'll get a corrupt log file. However, logback, log4j's successor, in prudent mode allows two appenders even in different JVMs to write to the same file.

0

It doesn't directly answer your question, but log4*net*'s FileAppender has a LockingModel attribute that you can set to only lock when the file is actually in use. So if you had two FileAppenders working in the same thread with MinimalLock set, it would probably work perfectly fine. On different threads, you might hit deadlock once in a while.

The FileAppender supports pluggable file locking models via 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 model, FileAppender.MinimalLock, only holds a write lock while the appender is writing a logging event.

A cursory web search didn't turn up any useful results about implementing MinimalLock in log4j.

2
  • log4net has nothing in common with log4j
    – skaffman
    Aug 10, 2009 at 11:24
  • Not true - it's rather similar in most concepts (error levels, appenders to name two).
    – ripper234
    Aug 10, 2009 at 17:04
0

From Log4j FAQ a3.3

How do I get multiple process to log to the same file?

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

As to what that actually means I will be investigating myself.

I also found the following workaround on another SO question:

Code + Example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.