Java Log4J RollingFileAppender 无法滚动文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24410984/
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
Log4J RollingFileAppender Unable to Roll File
提问by Joe
I am writing a small xml transformation layer in Java. I receive xml via web service, modify it, and then send the modified xml to another system. I then wait for a response and return the response to the original caller.
我正在用 Java 编写一个小的 xml 转换层。我通过 web 服务接收 xml,修改它,然后将修改后的 xml 发送到另一个系统。然后我等待响应并将响应返回给原始调用者。
System A -> Me -> System B -> Me -> System A
I want to log the request I receive, the request I send, the response I receive, and the request I send. Basically I want to log the xml where each arrow is in my diagram.
我想记录我收到的请求、我发送的请求、我收到的响应以及我发送的请求。基本上我想记录每个箭头在我的图表中的 xml。
My problem is with the RollingFileAppender. I try to roll at 10MB, sometimes it does and sometimes it doesn't roll. If it rolls a couple times, and then stops, it will continue to rename the rolled files from 3 to 4 and 4 to 5 and so on.
我的问题是 RollingFileAppender。我尝试以 10MB 的速度滚动,有时它会滚动,有时它不会滚动。如果它滚动几次,然后停止,它将继续将滚动的文件从 3 重命名为 4,将 4 重命名为 5,依此类推。
My best guess is that when the 10MB mark is crossed, there are multiple threads writing to the log file so the file cannot me renamed. I am hoping that Log4J has an easy solution for this, but if necessary, I am open to switching to a new logging framework. Thank you in advance for any help.
我最好的猜测是,当超过 10MB 标记时,有多个线程写入日志文件,因此无法重命名该文件。我希望 Log4J 有一个简单的解决方案,但如果有必要,我愿意切换到新的日志框架。预先感谢您的任何帮助。
EDITHere is my properties file.
编辑这是我的属性文件。
log4j.rootLogger=DEBUG, fileOut
log4j.appender.fileOut=org.apache.log4j.RollingFileAppender
log4j.appender.fileOut.File=/logs/log.log
log4j.appender.fileOut.layout=org.apache.log4j.PatternLayout
log4j.appender.fileOut.layout.ConversionPattern=%d %-5p %c - %m%n
log4j.appender.fileOut.MaxFileSize=10MB
log4j.appender.fileOut.MaxBackupIndex=10
log4j.appender.fileOut.append=true
EDIT 2This is essentially a bump, as this post has a low number of views. I feel like this cannot be a unique problem. Any help is much appreciated. Thanks!
编辑 2这本质上是一个颠簸,因为这篇文章的浏览量很低。我觉得这不可能是一个独特的问题。任何帮助深表感谢。谢谢!
采纳答案by Isaac
Log4J initializes itself at the classloader level. Within a certain classloader and its ancestors, Log4J can only be initialized once and the same Log4J configuration applies to all Log4J calls within the classloader.
Log4J 在类加载器级别初始化自己。在某个类加载器及其祖先中,Log4J 只能初始化一次,并且相同的 Log4J 配置适用于类加载器中的所有 Log4J 调用。
As long as all of your logging calls are performed within the same Log4J configuration "realm", Log4J knows how to synchronize access to the physical file pointed at by the rolling appender configuration; when the time comes to roll, rolling is performed with no problem.
只要您所有的日志记录调用都在同一个 Log4J 配置“领域”中执行,Log4J 就知道如何同步对滚动 appender 配置指向的物理文件的访问;到了滚动的时候,滚动就没有问题了。
Things become problematic once you have two (or more) Log4J "configuration realms" using the same physical file for the rolling appender configuration. That could be:
一旦您有两个(或更多)Log4J“配置领域”使用相同的物理文件进行滚动附加程序配置,事情就会变得有问题。那可能是:
- Two different web applications on the same physical JVM
- Two different web applications on two distinct JVMs
- Same web application horizontally clustered on two distinct JVMs
- 同一物理 JVM 上的两个不同的 Web 应用程序
- 两个不同的 JVM 上的两个不同的 Web 应用程序
- 同一 Web 应用程序水平集群在两个不同的 JVM 上
(etc)
(等等)
Log4J simply has no way of knowing who else, other than itself within the same Log4J configuration realm, uses that file. So, what ends up happening is that Log4J on System A attempts to roll the file (because it thinks that no other processes are accessing that file), and fails because someone on System B is using the file at the same time.
Log4J 根本无法知道除了同一个 Log4J 配置领域中的自己之外还有谁使用该文件。因此,最终发生的是系统 A 上的 Log4J 尝试滚动文件(因为它认为没有其他进程正在访问该文件),但由于系统 B 上的某人同时使用该文件而失败。
This is a known limitation of using file appenders, and you can't really blame Log4J for this: Log4J simply doesn't have the means of monitoring who else, other than Log4J in the same "configuration realm", is using that file.
这是使用文件附加程序的一个已知限制,您不能真正为此责怪 Log4J:Log4J 根本没有办法监视除同一“配置领域”中的 Log4J 之外的其他人正在使用该文件。
For such usage scenario, you can use the Log4J socket appender.
对于此类使用场景,您可以使用 Log4J socket appender。
If your scenario doesn't involve multiple Log4J "configuration realms", then try adding -Dlog4j.debug=true
to the JVM parameters and see what exactly is going on during the file rolling operation.
如果您的场景不涉及多个 Log4J“配置领域”,那么尝试添加-Dlog4j.debug=true
到 JVM 参数并查看文件滚动操作期间到底发生了什么。
回答by Rips
I also faced the same issue in my application.
我在我的申请中也遇到了同样的问题。
Thanks to @Isaac found that I was doing DOMConfigurator.configure for the same log configuration in 2 web applications deployed in the application server. I commented one of them and rolling over happened as expected.
感谢@Isaac 发现我正在为应用服务器中部署的 2 个 Web 应用程序中的相同日志配置执行 DOMConfigurator.configure。我评论了其中一个,并且按预期发生了翻车。
回答by Neil Stevens
For others that arrive here, check you are using RollingFileAppender NOT FileAppender!
对于到达这里的其他人,请检查您使用的是 RollingFileAppender 而不是 FileAppender!
Cut and paste errors are too easy, for me at least.
至少对我来说,剪切和粘贴错误太容易了。