简体   繁体   English

python中的RotatingFileHandler记录器打开多少个文件描述符?

[英]How many file descriptors does a RotatingFileHandler logger in python open?

Lets say I create a rotating file logger with python using RotatingFileHandler. 可以说我使用RotatingFileHandler用python创建了一个旋转文件记录器。 I set number of files to 10, and each file size to 1 MB. 我将文件数设置为10,每个文件大小设置为1 MB。 My question is related to how the rotation happens. 我的问题与轮换如何发生有关。 Is it that the rotation happens by keeping all the 10 files open and ensuring the contents of each file is modified as and when a line gets added to the latest file? 是否通过保持所有10个文件打开并确保在向最新文件添加一行时修改每个文件的内容来进行轮换? If that is so, then there should be at least 10 file descriptors that are always open. 如果是这样,那么应该至少有10个始终打开的文件描述符。

Say each file has 100 KB chunk rows, so each has just 10 rows. 假设每个文件有100 KB的块行,因此每个文件只有10行。 So when another row (of 100 KB) is inserted through this rotating log handler, wouldn't the last row of each file be put at the top of the next file (from newer to older)? 因此,当通过此旋转日志处理程序插入另一行(100 KB)时,是否将每个文件的最后一行放在下一个文件的顶部(从新到旧)? So it makes sense to keep all the file descriptors open all the time, doesn't it? 因此,使所有文件描述符始终保持打开状态很有意义,不是吗?

The handler only ever has one open file. 该处理程序只有一个打开的文件。 The other files are renamed when rotating. 旋转时,其他文件被重命名

First, the current file is closed. 首先,关闭当前文件。 Then, rotation renames files in reverse order; 然后,旋转以相反的顺序重命名文件; so the file with extension .9 is renamed to .10 first, deleting an already existing .10 if it is there. 所以有扩展名的文件.9重命名为.10首先,删除现有的.10 ,如果它的存在。 Then .8 is renamed to .9 , etc. Finally, the 'current' file is renamed to append the .1 extension. 然后将.8重命名为.9等。最后,将“当前”文件重命名为附加.1扩展名。

Depending on the delay flag, at the end a new file is opened, either when rotating or when the next log entry is to be written. 取决于delay标志,在旋转或要写入下一个日志条目时,最后都会打开一个新文件。

All this is included in the logging.handlers.RotatingFileHandler() documentation 所有这些都包含在logging.handlers.RotatingFileHandler()文档中

When the size is about to be exceeded, the file is closed and a new file is silently opened for output. 当将要超过该大小时,将关闭该文件,并以静默方式打开一个新文件以进行输出。 [...] If backupCount is non-zero, the system will save old log files by appending the extensions '.1', '.2' etc., to the filename. [...]如果backupCount不为零,则系统将通过在文件名后附加扩展名'.1','。2'等来保存旧的日志文件。 For example, with a backupCount of 5 and a base file name of app.log, you would get app.log , app.log.1 , app.log.2 , up to app.log.5 . 例如,在backupCount为5且基本文件名为app.log的情况下,您将获得app.logapp.log.1app.log.2app.log.5 The file being written to is always app.log . 写入的文件始终是app.log When this file is filled, it is closed and renamed to app.log.1 , and if files app.log.1 , app.log.2 , etc. exist, then they are renamed to app.log.2 , app.log.3 etc. respectively. 填充此文件后,将其关闭app.log.1命名为app.log.1 ,如果存在文件app.log.1app.log.2等,则它们将重命名为app.log.2 app.log.3等。

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

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