Log4J RollingFileAppender无法滚动文件 [英] Log4J RollingFileAppender Unable to Roll File

查看:144
本文介绍了Log4J RollingFileAppender无法滚动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java编写一个小的xml转换层。我通过Web服务接收xml,修改它,然后将修改后的xml发送到另一个系统。然后我等待响应并将响应返回给原始呼叫者。

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.

System A -> Me -> System B -> Me -> System A

我想记录我收到的请求,我发送的请求,收到的回复,和我发送的请求。基本上我想记录每个箭头在我的图表中的xml。

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.

我的问题在于RollingFileAppender。我尝试以10MB滚动,有时它会滚动,有时它不会滚动。如果它滚动了几次,然后停止,它将继续将滚动的文件重命名为3到4和4到5,依此类推。

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.

我最好的猜测是当超过10MB标记时,有多个线程写入日志文件,因此无法重命名该文件。我希望Log4J有一个简单的解决方案,但如果有必要,我愿意切换到新的日志框架。提前感谢您的帮助。

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.

编辑
这是我的属性文件。

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

编辑2 这实际上是一个颠簸,因为这篇文章的观看次数很少。我觉得这不是一个独特的问题。任何帮助深表感谢。谢谢!

EDIT 2 This 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!

推荐答案

Log4J在类加载器级别初始化自己。在某个类加载器及其祖先中,Log4J只能初始化一次,相同的Log4J配置适用于类加载器中的所有Log4J调用。

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知道如何同步访问滚动appender配置指向的物理文件;当滚动时,滚动执行没有问题。

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配置领域使用相同的物理文件,事情就会成问题用于滚动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:


  1. 同一物理JVM上的两个不同的Web应用程序

  2. 两个不同的Web应用程序在两个不同的JVM上

  3. 在两个不同的JVM上水平集群的相同Web应用程序

(等)

Log4J根本无法知道除了在同一个Log4J配置领域之外的其他人使用该文件。因此,最终发生的事情是系统A上的Log4J尝试滚动文件(因为它认为没有其他进程正在访问该文件),并且因为系统B上有人同时使用该文件而失败。

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根本无法监控除Log4J以外的其他人相同的配置领域,正在使用该文件。

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套接字appender。

For such usage scenario, you can use the Log4J socket appender.

如果您的方案不涉及多个Log4J配置域,那么尝试将 -Dlog4j.debug = true 添加到JVM参数中,看看究竟是什么在文件滚动操作期间继续。

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 RollingFileAppender无法滚动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆