同一台机器上的两个Tomcat实例使用同一个日志文件[英] Two instances of Tomcat on the same machine using the same log file

本文是小编为大家收集整理的关于同一台机器上的两个Tomcat实例使用同一个日志文件的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我最近遇到了一个问题,即我的日志文件没有像应该在我的log4j配置中定义的那样滚动.我发现罪魁祸首是我有两个实例运行了同一应用程序,它们都持有相同的日志文件,因此,由于另一个文件,我都无法将其滚动.

但是,我仍然想使用相同的日志文件.我使用两个实例进行负载平衡,每个实例都有一个日志文件很烦人.

有什么办法可以做到这一点?还是我注定要有多个日志文件?

推荐答案

i 建议使用相同对 多个应用程序的日志文件.

  • 很难阅读日志文件.
  • 它将影响性能.

但是,您可以通过使用'如下记录.

<appender name="FILE_PRUDENT" class="ch.qos.logback.core.FileAppender">
    <file>logs/test.log</file>
    <prudent>true</prudent>
</appender>

来自官方文档,

在审慎模式下,fileappender将安全地写入指定的文件, 即使在其他 fileappender实例的情况下也在 不同的 JVM,有可能在不同的主机上运行.默认值 审慎模式的价值是错误的.

本文地址:https://www.itbaoku.cn/post/1575153.html

问题描述

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?

推荐答案

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.