java Log4j 与 Logback:并发写入同一日志?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11926306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:57:35  来源:igfitidea点击:

Log4j vs Logback: concurrent writing to same log?

javaweb-applicationslogginglog4jlogback

提问by Shayan

I have several web applications running on the same tomcat.

我有几个 Web 应用程序在同一个 tomcat 上运行。

I have two questions:

我有两个问题:

1- By searching, I understood that when multiple applications are present, logging into the same file might make some problems. Is that the case for multiple applications running on the same web server? Is that also correct when default stdout output is used?

1- 通过搜索,我了解到当存在多个应用程序时,登录同一个文件可能会出现一些问题。在同一 Web 服务器上运行的多个应用程序是否也是这种情况?使用默认 stdout 输出时,这是否也正确?

2- In Logback library there is a prudent mode:

2- 在 Logback 库中有一个谨慎的模式:

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.

在谨慎模式下,FileAppender 将安全地写入指定的文件,即使存在其他 FileAppender 实例在不同的 JVM 中运行,可能运行在不同的主机上。谨慎模式的默认值为 false。

I want to know if using Logback is only favorable on multiple JVMs or it is also advantageous for multiple we applications running on the same web server? If not, is it identical to log4j in this aspect?

我想知道使用 Logback 是只对多个 JVM 有利,还是对在同一 Web 服务器上运行的多个应用程序也有利?如果不是,它在这方面是否与 log4j 相同?

Thanks

谢谢

回答by Ceki

In both log4j and logback if multiple FileAppenderinstances write to the same log file, there is a highrisk for the said log file becoming corrupt. Whether the FileAppenderinstances run on the same JVM or different JVMs is irrelevant, i.e. the risk of corruption is the same.

在 log4j 和 logback 中,如果多个FileAppender实例写入同一个日志文件,则所述日志文件损坏的风险很高。无论是FileAppender在同一个JVM或者不同的JVM运行的实例是无关紧要的,腐败的,即风险是一样的。

As mentioned in the docs, in prudent modelogback's FileAppenderwill avoid corruption, even in the presence of other FileAppenderinstances running in the same or different JVMs, potentially running on different hosts. By default, prudent mode is disabled.

正如文档中所提到的,在谨慎模式下FileAppender,即使存在FileAppender运行在相同或不同 JVM 中(可能运行在不同主机上)的其他实例,logback也将避免损坏。缺省情况下,谨慎模式处于关闭状态。

The console cannot be corrupted so the question is moot.

控制台不会被损坏,所以这个问题没有实际意义。

回答by fglez

There's one thing which must be clarified: There will be problems when different instancesof Log4j are writing to the same fileconcurrently, whether running in the same JVM or not.

有一点必须澄清:当不同的 Log4j实例同时写入同一个文件时,无论是否在同一个 JVM 中运行,都会出现问题。

When using servers (and different classloaders) it is possible to have a single server-wide instance or multiple instances of Log4j, depending on deployment and configuration.

当使用服务器(和不同的类加载器)时,可能有一个服务器范围的实例或多个 Log4j 实例,这取决于部署和配置。

  1. See above. Stdout can suffer the same mixed output problem, but not when rotating files.
  2. Logback's prudent mode would address concurrent writing by different instances (same JVM or not). If your configuration uses a server-wide Log4j instance there's no advantage for this aspect.
  1. 往上看。Stdout 可能会遇到相同的混合输出问题,但在旋转文件时不会。
  2. Logback 的谨慎模式将解决不同实例(相同 JVM 或不同 JVM)的并发写入问题。如果您的配置使用服务器范围的 Log4j 实例,则这方面没有优势。

回答by Sebastian van Wickern

Using Filelocks is never actually efficient/secure, so while logging to the same file from different appenders/JVM's works, it is not recommended. See the configuration which I took directly from logback-appenders-faq.

使用 Filelocks 实际上从来都不是高效/安全的,因此当从不同的 appender/JVM 的作品记录到同一个文件时,不建议这样做。查看我直接从logback-appenders-faq 获取的配置

<configuration>
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <!-- Support multiple-JVM writing to the same log file -->
    <prudent>true</prudent>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
      <maxHistory>30</maxHistory> 
    </rollingPolicy>

    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender> 

  <root level="DEBUG">
    <appender-ref ref="FILE" />
  </root>
</configuration>

Your other options for multiple JVMs writing to some unified source are SocketAppendersand the JDBCAppender.

多个 JVM 写入某个统一源的其他选项是SocketAppendersJDBCAppender

The JDBCAppenderwill be completely replaced in the future though and is not recommendedeither. See logbacks mailinglist.

JDBCAppender将在未来虽然被完全取代,并且不建议两种。请参阅 logbacks邮件列表

SocketAppenders might be a little raw, as you probably weren't planing on writing much code for logback.

SocketAppenders 可能有点原始,因为您可能不打算为 logback 编写很多代码。

There is one more option. You could use something like clusterlog, which has been build to solve exactly the kind of problem you have.

还有一种选择。您可以使用诸如clusterlog之类的东西,它的构建是为了完全解决您遇到的问题。

回答by Teh Hippo

  1. Yes. In general, your principle should be that you write a different log file for each application (web or not). The alternative, is that you leave it up to the server to decide on the file to write to. JBoss has ways to genericly log out, which will go to the same file. However, I still prefer to have a separate log for each application.
  2. As far as I know, both log4j and Logback aim to be minimal in the overhead they can cause the application, so there's unlikely to be one that's more favourable. If multiple JVMs is essential for you, I believe Logback is better at handling it, but using it outside this is not a bad idea.
  1. 是的。通常,您的原则应该是为每个应用程序(无论是否为 Web)编写不同的日志文件。另一种方法是让服务器决定要写入的文件。JBoss 有一般注销的方法,这将转到同一个文件。但是,我仍然更喜欢为每个应用程序创建一个单独的日志。
  2. 据我所知,log4j 和 Logback 的目标是将它们可能导致应用程序的开销降至最低,因此不太可能有一个更有利的。如果多个 JVM 对你来说是必不可少的,我相信 Logback 更擅长处理它,但在这之外使用它并不是一个坏主意。