简体   繁体   English

Log4J RollingFileAppender无法滚动文件

[英]Log4J RollingFileAppender Unable to Roll File

I am writing a small xml transformation layer in Java. 我正在用Java编写一个小的xml转换层。 I receive xml via web service, modify it, and then send the modified xml to another system. 我通过Web服务接收xml,修改它,然后将修改后的xml发送到另一个系统。 I then wait for a response and return the response to the original caller. 然后我等待响应并将响应返回给原始调用者。

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. 我的问题在于RollingFileAppender。 I try to roll at 10MB, sometimes it does and sometimes it doesn't roll. 我尝试以10MB的速度滚动,有时它会滚动,有时它不滚动。 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. 如果它滚动了几次,然后停止,它将继续将滚动的文件从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. 我最好的猜测是,当超过10MB标记时,有多个线程写入日志文件,因此我无法重命名该文件。 I am hoping that Log4J has an easy solution for this, but if necessary, I am open to switching to a new logging framework. 我希望Log4J有一个简单的解决方案,但如果有必要,我愿意切换到新的日志框架。 Thank you in advance for any help. 预先感谢您的任何帮助。

EDIT Here 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 2 This is essentially a bump, as this post has a low number of views. 编辑2这实际上是一个颠簸,因为这篇文章的观看次数很少。 I feel like this cannot be a unique problem. 我觉得这不是一个独特的问题。 Any help is much appreciated. 任何帮助深表感谢。 Thanks! 谢谢!

Log4J initializes itself at the classloader level. Log4J在类加载器级别初始化。 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调用。

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; 只要所有日志记录调用都在相同的Log4J配置“领域”中执行,Log4J就知道如何同步对滚动appender配置指向的物理文件的访问; when the time comes to roll, rolling is performed with no problem. 当滚动时,滚动执行没有问题。

Things become problematic once you have two (or more) Log4J "configuration realms" using the same physical file for the rolling appender configuration. 一旦你有两个(或更多)Log4J“配置领域”使用相同的物理文件进行滚动appender配置,就会出现问题。 That could be: 那可能是:

  1. Two different web applications on the same physical JVM 同一物理JVM上的两个不同的Web应用程序
  2. Two different web applications on two distinct JVMs 两个不同JVM上的两个不同的Web应用程序
  3. Same web application horizontally clustered on two distinct JVMs 相同的Web应用程序水平集群在两个不同的JVM上

(etc) (等等)

Log4J simply has no way of knowing who else, other than itself within the same Log4J configuration realm , uses that file. Log4J根本无法知道在同一个Log4J配置域中除了它本身之外还有谁使用该文件。 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. 因此,最终发生的事情是系统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套接字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参数,并查看文件滚动操作期间到底发生了什么。

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. 对我来说,剪切和粘贴错误太容易了。

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. 感谢@Isaac发现我正在为应用服务器中部署的2个Web应用程序中的相同日志配置执行DOMConfigurator.configure。 I commented one of them and rolling over happened as expected. 我对其中一个进行了评论,并按预期发生了滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM