Log4j stops logging in a File(File Appenders) when i open/edit the File?

喜夏-厌秋 提交于 2019-12-25 02:16:32

问题


I use log4j in my java application.

My log4j configuration file

Set root logger level.

log4j.rootLogger=WARN, logfile

log4j.appender.logfile.MaxFileSize=20MB backup file

log4j.appender.logfile.MaxBackupIndex=2

log4j.appender.logfile=org.apache.log4j.RollingFileAppender

log4j.appender.logfile.File=${log.output.file}

log4j.appender.logfile.layout=org.apache.log4j.PatternLayout

log4j.appender.logfile.layout.ConversionPattern=%-5p %d [%t] %c{2}: %m%n

log.output.File refers to the logging File.

I run multiple threads in a program that writes logging entries periodically to the output File. If I manually open the file and edit the contents of the file when the program is running .log4j stops logging. I dont want the logging to stop; I am new to Log4j.


回答1:


Depending on the editor, there are two ways in which a file is saved:

  1. The new file contents is saved to a temporary file first, and when everything has been saved successfully, the new file is renamed to the real file. This has the benefit that in case of an IO error, the old content is still there. And if some other process reads the original file in between, it will never see a mixture of the old and new content.

  2. The original file is opened, and the new contents is written directly into the file. This has the benefit of using less disk space (because no temporary file is needed). A disadvantage is that another process may have the same file opened in append mode (maybe for logging), so when the new content is written in multiple pieces, the writes may overlap, and the file contents is corrupt.

There are a lot of other consequences that depend on these two different ways, which I am too lazy to write down right now.

Because of these considerations, gedit seems to have chosen the first way. To be sure, you can do two things:

  1. Inspect the source code of gedit, especially the function that saves a buffer to a file.
  2. Run strace -o strace.out gedit logfile.log, edit something in the file and save the file. Exit gedit, and have a look at the file strace.out. Search for the name of the log file, and see which system calls (read, open, close, rename, write, stat, fstat) happen during the save action.


来源:https://stackoverflow.com/questions/8142465/log4j-stops-logging-in-a-filefile-appenders-when-i-open-edit-the-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!