0
votes

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
On which operating system are you? Which editor do you use?Roland Illig
gedit Text Editor and Ubuntu Linuxuser1048371

1 Answers

2
votes

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.