I've 2 webapplications, want to log the log messages of these two web apps into only one log file. I tried this scenario, facing the issue as "If one web app logs the message into log, the second web app is not able log the message into log file". If I stop the server, the second app is able to log.Any help?
3 Answers
writing to the same file from multiple independant processes is a bad idea - only one of them can get a file lock, as evident from your issues.
what you need is a centralized logging server and have all of your applications log to that server over the network. see this question
-
As far as I know only one process can get an _exclusive_ lock - there is also the `O_APPEND` mode (see [details](http://linux.about.com/od/commands/l/blcmdl2_open.htm)). But this does not change that it is still not a good idea to mix log files ;-) – Jost Aug 27 '13 at 08:25
-
@Jost - that will depend greatly on the logging framework used and the underlying operating system and, even if it works, you'll get interleaved log lines. it'll be a mess no matter what :-) – radai Aug 27 '13 at 08:29
You can use Log4J's SocketAppender
for that, which is much cleaner - an example can be found in this article: log4j: How to use SocketAppender.
To be honest it is a bit overkill compared to having two independent logfiles for your processes.
Btw. your approach might have problems on distributed filesystems (e.g. NFS) - don't mix logfiles.
Hope that helped a bit. *Jost
It is not good idea to put logging of two different applications to common log. Logging module lock the log file for writing. if other application is trying to access the same log, it wont get the lock. I would avoid such things.

- 73
- 6