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

j vickroy jim.vickroy at noaa.gov
Thu Nov 13 17:07:21 EST 2003


Hello,

I'm trying to understand the behavior of the Python 2.3 logging module (MS
Windows 2k) with regard to RotatingFileHandler.  The following script
illustrates a puzzling problem.  What is wrong with this script?

Thanks,

-- jv

BEGIN FILE _________________________________________
'''
This script terminates as follows:

Traceback (most recent call last):
  File "D:\$PROJECTS\experimental\Py Logging\t_xb.py", line 63, in ?
    shutdown()
  File "C:\Python23\lib\logging\__init__.py", line 1195, in shutdown
    h.flush()
  File "C:\Python23\lib\logging\__init__.py", line 661, in flush
    self.stream.flush()
ValueError: I/O operation on closed file

What is wrong with it?
'''



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 FILE _________________________________________






More information about the Python-list mailing list