两个 log4j fileappender 可以写入同一个文件吗?
暂时忘记为什么你会做这样的事情——如果出于某种原因,两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
它不会直接回答您的问题,但 log4*net* 的 FileAppender 有一个 LockingModel 属性,您可以将其设置为仅在文件实际使用时锁定。 因此,如果您有两个 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.
A cursory web search didn't turn up any useful results about implementing MinimalLock in log4j.
来自 Log4j 常见问题解答 a3.3
我该如何让多个进程登录到同一个文件?
至于这实际上意味着什么,我将亲自调查。
我还在另一个 SO问题:
代码 + 示例
From Log4j FAQ a3.3
How do I get multiple process to log to the same file?
As to what that actually means I will be investigating myself.
I also found the following workaround on another SO question:
Code + Example