This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Logging Module - followfile patch
Type: Stage:
Components: Extension Modules Versions: Python 2.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: vinay.sajip Nosy List: cjschr, georg.brandl, vinay.sajip
Priority: normal Keywords: patch

Created on 2006-11-17 15:44 by cjschr, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
logging.patch cjschr, 2006-11-20 17:06 followfile patch (ST_SIZE-based)
Messages (9)
msg51366 - (view) Author: chads (cjschr) Date: 2006-11-17 15:44
Pertaining to the FileHandler and the file being written to:

It's possible that the file being written to will be
rolled-over by an external application such as newsyslog.
By default, FileHandler tracks the file descriptor, 
not the file.  If the original file is renamed, the file 
descriptor is still updated; however, it's probably 
desired that continued updates to the original file take 
place instead.

This patch adds an attribute to the FileHandler class
constructor (and basicConfig kw as well).  If the 
attribute evaluates to True, the filename, not the 
descriptor is tracked.  Basically, the code compares the 
file status from a previous emit call to the current call
before the base class emit is called.  If a difference in
st_ino or st_dev is found, the current stream is 
flush/closed and a new one, based on baseFilename, is
created, file status is updated, and then the base class 
emit is called.
msg51367 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-11-18 19:14
Assigning to Vinay.
msg51368 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2006-11-19 20:32
This patch, relying as it does on Unix-specific details such as i-nodes, does not appear as if it will work under Windows. For that reason I will mark it as Pending and Invalid for now, if cjschr can update this tracker item with how the patch will work on Windows, I will look at it further. The SF system will automatically close it if no update is made to the item in approx. 2 weeks, though it can still be reopened after that.
msg51369 - (view) Author: chads (cjschr) Date: 2006-11-20 17:02
Updated per vsajip to work on Windoze too.  The code now
checks for a current size < previous size (based on ST_SIZE).
msg51370 - (view) Author: chads (cjschr) Date: 2006-11-20 17:06
Uploaded the wrong diff.  This is the correct one.
msg51371 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2007-01-11 21:50
I've had a bit more of a think about this, and realised that I made a boo-boo in one of my earlier comments. Under Windows, log files are opened with exclusive locks, so that other processes cannot rename or move files which are open. So I believe the approach won't work at all under Windows. (Chad, sorry about making you redo the patch with ST_SIZE rather than ST_DEV and ST_INO).

I also think this is a less common use case than warrants supporting it at the basicConfig() level, which is for really very basic usage configuration. So I would advocate adding a WatchedFileHandler (in logging.handlers) which watches st_dev and st_ino (as per Chad's original patch) and closes the old file descriptor and reopens the file when a change is seen. Some recent changes checked into SVN trunk facilitate the reopening - I've added an _open() method to FileHandler to do this.

Chad, what do you think of this approach?
msg51372 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2007-01-14 21:57
WatchedFileHandler added to logging.handlers, checked into trunk. Documentation updated, too.
msg51373 - (view) Author: chads (cjschr) Date: 2007-01-15 15:43
I like the implementation Vinay.  Nice work.

msg51374 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2007-01-15 16:29
You're welcome, Chad. Thanks for the idea. Closing this item.
Date User Action Args
2022-04-11 14:56:21adminsetgithub: 44247
2006-11-17 15:44:29cjschrcreate