两个 log4j fileappender 可以写入同一个文件吗?

发布于 2024-07-30 12:52:30 字数 69 浏览 4 评论 0原文

暂时忘记为什么你会做这样的事情——如果出于某种原因,两个 FileAppender 配置了同一个文件——这个设置会起作用吗?

Forget for a second the question of why on earth would you do such a thing - if, for whatever reason, two FileAppenders are configured with the same file - will this setup work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

皇甫轩 2024-08-06 12:52:30

Log4j 的 FileAppender 不允许两个 JVM 写入同一文件。 如果您尝试,您将得到一个损坏的日志文件。 然而,logback,log4j的后继者,在谨慎模式中允许两个appender,即使在不同的情况下也是如此。 JVM 写入同一文件。

Log4j's FileAppender does not allow for two JVM's writing to the same file. If you try, you'll get a corrupt log file. However, logback, log4j's successor, in prudent mode allows two appenders even in different JVMs to write to the same file.

贵在坚持 2024-08-06 12:52:30

它不会直接回答您的问题,但 log4*net* 的 FileAppender 有一个 LockingModel 属性,您可以将其设置为仅在文件实际使用时锁定。 因此,如果您有两个 FileAppender 在设置了 MinimalLock 的同一个线程中工作,那么它可能会工作得很好。 在不同的线程上,您可能偶尔会遇到死锁。

FileAppender 通过 LockingModel 属性支持可插入文件锁定模型。 由 FileAppender.ExclusiveLock 实现的默认行为是获取文件上的独占写入锁定,直到关闭此附加程序。 替代模型 FileAppender.MinimalLock 仅在附加程序写入日志记录事件时持有写入锁。

粗略的网络搜索没有找到任何关于在 log4j 中实现 MinimalLock 的有用结果。

It doesn't directly answer your question, but log4*net*'s FileAppender has a LockingModel attribute that you can set to only lock when the file is actually in use. So if you had two FileAppenders working in the same thread with MinimalLock set, it would probably work perfectly fine. On different threads, you might hit deadlock once in a while.

The FileAppender supports pluggable file locking models via the LockingModel property. The default behavior, implemented by FileAppender.ExclusiveLock is to obtain an exclusive write lock on the file until this appender is closed. The alternative model, FileAppender.MinimalLock, only holds a write lock while the appender is writing a logging event.

A cursory web search didn't turn up any useful results about implementing MinimalLock in log4j.

岛徒 2024-08-06 12:52:30

来自 Log4j 常见问题解答 a3.3

我该如何让多个进程登录到同一个文件?

您可以将每个进程日志记录到 SocketAppender。 接收方SocketServer(或SimpleSocketServer)可以接收所有事件并将它们发送到单个日志文件。

至于这实际上意味着什么,我将亲自调查。

我还在另一个 SO问题

代码 + 示例

From Log4j FAQ a3.3

How do I get multiple process to log to the same file?

You may have each process log to a SocketAppender. The receiving SocketServer (or SimpleSocketServer) can receive all the events and send them to a single log file.

As to what that actually means I will be investigating myself.

I also found the following workaround on another SO question:

Code + Example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文