简体   繁体   English

在应用程序关闭之前,log4j RollingFileAppender不会写入日志

[英]log4j RollingFileAppender not writing to log until application close

We're using log4j 1.2.17 via slf4j-log4j12 1.7.7 with Java 8. I'm running the application from within Eclipse 4.4.1 on Windows 7 Professional 64-bit. 我们正在Java 8上通过slf4j-log4j12 1.7.7使用log4j 1.2.17。我正在Windows 7 Professional 64位上的Eclipse 4.4.1中运行该应用程序。

Our root log4j.properties file looks like this: 我们的根log4j.properties文件如下所示:

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p (%c{1}) [%d] - %l: %m%n

In the application we set up a rolling file appender based upon the configuration: 在应用程序中,我们根据配置设置了滚动文件追加器:

EnhancedPatternLayout patternLayout = new EnhancedPatternLayout("%-5p (%c{1}) [%d] - %l: %m%n");
RollingFileAppender rollingFileAppender = new RollingFileAppender(patternLayout, logPath, true);
rollingFileAppender.setMaxBackupIndex(5);
rollingFileAppender.setMaxFileSize(5MB);
Logger.getRootLogger().addAppender(rollingFileAppender);

That works nicely... when the application is finally closed! 效果很好...当应用程序最终关闭时! While the application is running the log file sits at zero bytes, which isn't very helpful. 当应用程序运行时,日志文件位于零字节,这不是很有帮助。

(In this version of our application we switched from using straight log4j to using slf4j->log4j. I don't remember offhand if the behavior was different before we switched.) (在此版本的应用程序中,我们从使用直线log4j切换为使用slf4j-> log4j。如果切换之前的行为不同,我不记得使用了。)

Update: I've traced through the creation of the actual writer. 更新:我已经追溯了实际作者的创建过程。 log4j creates a FileOutputStream , then wraps that in an OutputStreamWriter , which it then wraps in a CountingQuietWriter which is called by the appender. log4j创建一个FileOutputStream ,然后将其包装在OutputStreamWriter ,然后将其包装在一个由追加程序调用的CountingQuietWriter中。 The bufferedIO flag is turned off. bufferedIO标志关闭。 I have also traced through writing a log message, and it seems to be written normally to the CountingQuietWriter -> OutputStreamWriter -> FileOutputStream . 我还通过编写日志消息进行了跟踪,并且似乎通常将其写入CountingQuietWriter > OutputStreamWriter > FileOutputStream Yet nothing appears in the log file until I close the application. 在关闭应用程序之前,日志文件中什么也没有显示。

Why isn't the application writing to the log immediately, or at least every so often after the buffer gets full? 为什么应用程序不立即写日志,或者至少在缓冲区满后经常写日志?

You can try this, forcing the appender to write each line whenever it's logged by the application : 您可以尝试执行此操作,迫使附加器在应用程序每次记录时写出每一行:

rollingFileAppender.setImmediateFlush(true);
rollingFileAppender.setBufferedIO(false);

The weird thing is that these values are the default one, but you can try it anyway. 奇怪的是这些值是默认值,但是您仍然可以尝试一下。

In my experience, this could very well just be your OS/filesystem/storage appliance/etc issue, or the application your using to view the file size, not retrieving/replicating/updating file metadata...and the data might actually be there. 以我的经验,这很可能只是您的OS /文件系统/存储设备/等问题,或者是您用来查看文件大小的应用程序,而不是检索/复制/更新文件元数据...并且数据可能实际上在那里。 The file system may only replicate/report metadata after your write lock is released, for example. 例如,文件系统只能在释放写锁定后复制/报告元数据。

When you open the file (read-only) while the application is running, do you see data, despite the reported file size? 当应用程序运行时打开文件(只读)时,尽管报告了文件大小,您是否仍看到数据?

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

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