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.

classification
Title: Logging failes for rotating file log (during rollOver)
Type: Stage:
Components: Library (Lib) Versions: Python 2.3
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: vinay.sajip Nosy List: smeyers2002, tim.peters, vinay.sajip
Priority: normal Keywords:

Created on 2004-04-22 07:25 by smeyers2002, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
log.py smeyers2002, 2004-04-22 07:25 Python script that generates the descibed error
Messages (3)
msg20557 - (view) Author: smeyers2002 (smeyers2002) Date: 2004-04-22 07:25
I have a problem with the python logging. The attached 
python file 'log.py' initializes the python logging (rotating 
log file) and starts writing lines in a log file.

Everything works, if the line 'log.refresh' is commented 
out. If the line is not commented out, I always get an 
error by the first rollOver. So each time when I re-
initialize the logging (=method refresh in my code) I get 
this error (see below). What can be the problem? 
OS information: Windows XP Professional, version 
5.1.2600 Service Pack 1

Traceback (most recent call last):
  File "log.py", line 21, in ?
    log.logger.info(" I am line "+str(i))
  File "C:\Python23\lib\logging\__init__.py", line 893, in 
info
    apply(self._log, (INFO, msg, args), kwargs)
  File "C:\Python23\lib\logging\__init__.py", line 994, in 
_log
    self.handle(record)
  File "C:\Python23\lib\logging\__init__.py", line 1004, in 
handle
    self.callHandlers(record)
  File "C:\Python23\lib\logging\__init__.py", line 1037, in 
callHandlers
    hdlr.handle(record)
  File "C:\Python23\lib\logging\__init__.py", line 592, in 
handle
    self.emit(record)
  File "C:\Python23\lib\logging\handlers.py", line 105, in 
emit
    self.doRollover()
  File "C:\Python23\lib\logging\handlers.py", line 90, in 
doRollover
    os.rename(self.baseFilename, dfn)
OSError: [Errno 13] Permission denied
--------------------------------
PS: Sometimes I also get the error 'ValueError: I/O 
operation on closed file'
msg20558 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2004-04-22 21:51
Logged In: YES 
user_id=31435

Windows is unique in that it will not allow renaming (or 
deleting) a file that's open.  I expect that by calling refresh() 
all the time, you're creating additional new objects that have 
C:\log.txt open, so that it's impossible for rollover to rename 
C:\log.txt on Windows.  There's nothing the logging package 
can do about that.

You're also adding an endless number of redundant handlers 
to the same logger.  Try this:

1.  In your __init__ method, set

     self.logger = None

2. Start your refresh() method with

        if self.logger is not None:
            self.rotatingFileLogHandler.close()
            self.logger.removeHandler(self.rotatingFileLogHandler)

Closing the old handler arranges to close the C:\log.txt file, so 
that it's *possible* to rename it on Windows.  Removing the 
old handler from the logger should prevent seeing an 
unpredictable number of copies of individual log msgs, and 
probably prevent some "I/O operation on closed file" screwups 
too.
msg20559 - (view) Author: smeyers2002 (smeyers2002) Date: 2004-04-29 08:55
Logged In: YES 
user_id=1026630

Thanks Tim, this solved the problem.
History
Date User Action Args
2022-04-11 14:56:03adminsetgithub: 40174
2004-04-22 07:25:10smeyers2002create