本文介绍了当我打开/编辑文件时,Log4j停止登录文件(文件添加程序)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java应用程序中使用log4j.

I use log4j in my java application.

我的log4j配置文件

My log4j configuration file

log4j.rootLogger = WARN,日志文件

log4j.rootLogger=WARN, logfile

log4j.appender.logfile.MaxFileSize = 20MB备份文件

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

log4j.appender.logfile.MaxBackupIndex = 2

log4j.appender.logfile.MaxBackupIndex=2

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

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

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

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

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

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

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

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

log.output.File是指日志文件.

我在一个程序中运行多个线程,该程序定期将日志条目写入输出文件.如果我在程序运行时手动打开文件并编辑文件内容,则log4j会停止记录.我不想停止记录;我是 Log4j 的新手.

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.

推荐答案

根据编辑器,有两种保存文件的方式:

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

  1. 新文件的内容首先保存到临时文件中,当所有内容成功保存后,新文件将重命名为真实文件.这样的好处是,在发生IO错误的情况下,旧内容仍然存在.而且,如果其他进程读取了两者之间的原始文件,则将永远不会看到新旧内容的混合.

  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.

打开原始文件,并将新内容直接写入文件中.这样的好处是使用更少的磁盘空间(因为不需要临时文件).缺点是另一个进程可能在追加模式下打开了相同的文件(可能用于记录),因此,当新内容以多段形式写入时,写入可能会重叠,并且文件内容会损坏.

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.

由于这些考虑,gedit似乎选择了第一种方法.可以肯定的是,您可以做两件事:

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

  1. 检查gedit的源代码,尤其是将缓冲区保存到文件的功能.
  2. 运行 strace -o strace.out gedit logfile.log ,编辑文件中的内容并保存文件.退出gedit,然后查看文件 strace.out .搜索日志文件的名称,然后查看在保存操作期间发生了哪些系统调用(读取,打开,关闭,重命名,写入,stat,fstat).
  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.

这篇关于当我打开/编辑文件时,Log4j停止登录文件(文件添加程序)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 12:44