logging.shutdown() ValueError: I/O operation on closed file

j vickroy robinjim at earthlink.net
Thu Nov 13 20:32:36 EST 2003


Here is the result of not closing a handler manually -- Peter's suggestion
(3)

Traceback (most recent call last):
  File
"C:\Python23\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "E:\$PROJECTS\experimental\Py Logging\t_xc.py", line 61, in ?
    log.error('testing Python logging module')
  File "C:\Python23\lib\logging\__init__.py", line 923, in error
    apply(self._log, (ERROR, 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

and here is the script that generated the above behavior:

begin -------------------------------------------------
from   logging import getLogger, Formatter, shutdown, DEBUG, INFO, WARNING,
ERROR, CRITICAL
from   logging.handlers import RotatingFileHandler



def logger_for(component):
   '''
   RETURNS a logger for the specified component.

   SIDE-EFFECTS
      (re)assigns the logger handler
   '''

   global handler

   logger = getLogger(component)

   if handler:
##      handler.flush()
##      handler.close()
      logger.removeHandler(handler)

   # In normal, operational mode, the following parameters:
   filename    = '%s.log'%component
   mode        = 'a'
   maxBytes    = 100
   backupCount =   5
   # would be user-configurable "on the fly" hence the reason for this
function.

   handler = RotatingFileHandler(filename, mode, maxBytes, backupCount)

   handler.setLevel(DEBUG)

   logger.addHandler(handler)

   return logger



handler = None

for i in range(20):
   log = logger_for('supplier')
   log.error('testing Python logging module')

shutdown()
end -----------------------------------------







More information about the Python-list mailing list