问题
I recently had an issue where my log files were not rolling over like they were supposed to as defined in my log4j config. I figured out that the culprit was that I have two instances Tomcat running the same app which both had a hold of the same log file, so neither one could roll it over because of the other one.
However, I would still like to use the same log file. I use two instances for load balancing and it would be annoying to have a log file for each instance.
Is there any way I can do this? Or am I doomed to have multiple log files?
回答1:
I would not recommend to use same log file for multiple applications.Because,
- it will be difficult to read log file.
- it will impact performance.
However, you can achieve this by using 'prudent' flag from logback as below.
<appender name="FILE_PRUDENT" class="ch.qos.logback.core.FileAppender">
<file>logs/test.log</file>
<prudent>true</prudent>
</appender>
From official docs,
In prudent mode, FileAppender will safely write to the specified file, even in the presence of other FileAppender instances running in different JVMs, potentially running on different hosts. The default value for prudent mode is false.
来源:https://stackoverflow.com/questions/42836175/two-instances-of-tomcat-on-the-same-machine-using-the-same-log-file