views:

92

answers:

1

We have a simple Webapp running on two Tomcat instances behind Apache (a Tomcat HA setup using mod_proxy_balancer and mod_proxy_ajp). The role of the webapp is to write incoming request data to a log file.

Given that each tomcat instance is running in it's own JVM, how should we implement the log file writing?

It would be excellent if both instances could write to the same file, but it would seem Log4J does not support that. We are open to writing our own file writer, but I am unsure if a java.nio.channels.FileLock will do what we want.

Underneath is Debian with an ext3 file system, so if we were doing this in C or even PHP, we would use file system locks. However Java was written to abstract away the underlying system, and has grown to be a mess of classes in multiple packages all claiming to be able to write files in one way or another. Does anyone have any advice? Are there any packages out there that could help us?

Is it just a dumb dream to want to write to the same file from 2 JVMs in the first place? Surely I'm not the first.

Another requirement is for these files to be rolled every hour, but that is not as much of an issue (except maybe at file creation time).

Thanks!

+2  A: 

It would be a far, far simpler solution to simply write to two (or more) log files and then just combine then after you roll them.

Writing to the one file from two or more processes has all sorts of issues. Locking the file is a fairly coarse-grained and heavyweight solution.

An alternative might simply be to write the log messages to a queue (JMS) and have one process read off the messages and write to a single log file.

cletus
Yeah I know... but it means the logic is not all in one place... pros and cons I guess.
Peter Sankauskas
What logic? You're writing to a log file, right? Or is there more to it?
cletus
+1 for the queue idea, that way any incoming requests don't have to wait while the file IO takes place for your webapp to respond
matt b