ValueError: I/O operation on closed file

dj d.a.abernathy at gmail.com
Mon Apr 13 19:44:44 EDT 2009


On Apr 11, 12:30 am, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> On Fri, 10 Apr 2009 13:25:25 -0700 (PDT), dj <d.a.aberna... at gmail.com>
> declaimed the following in gmane.comp.python.general:
>
>
>
> > I have a handler which I use with a set of log levels for the python
> > logging module.
>
> > ----------------------------------- myhandler.py
> > --------------------------------------------------------
> > import logging.handlers
>
> > # create my handler class
> > class MyHandler(logging.handlers.RotatingFileHandler):
> >     def __init__(self, fn):
> >         logging.handlers.RotatingFileHandler.__init__(self, fn,
>
> > maxBytes=10485760, backupCount=5)
>
> > # Register handler in the "logging.handlers" namespace
> > logging.handlers.MyHandler = MyHandler
>
>         Don't you need to pass an INSTANCE of the handler? Or SOMEWHERE
> create an instance for use. Note that the filename is one of the
> arguments needed to initialize this, and since we see no code with a
> root file name ... ???
>
>         No addHandler() anywhere?
>
> > --------------------------------------------------------------------------------------------------------------------
>
> > Using it, repeatedly generates this error:
>
> > Traceback (most recent call last):
> >   File "C:\python26\lib\logging\handlers.py", line 74, in emit
> >     if self.shouldRollover(record):
> >   File "C:\python26\lib\logging\handlers.py", line 146, in
> > shouldRollover
> >     self.stream.seek(0, 2)  #due to non-posix-compliant Windows
> > feature
> > ValueError: I/O operation on closed file
>
>         That's a rather short traceback -- where are the calls to the logger
> that would trigger the emit() call? Where do you specify that this
> handler is suppose to be used
> --
>         Wulfraed        Dennis Lee Bieber               KD6MOG
>         wlfr... at ix.netcom.com             wulfr... at bestiaria.com
>                 HTTP://wlfraed.home.netcom.com/
>         (Bestiaria Support Staff:               web-a... at bestiaria.com)
>                 HTTP://www.bestiaria.com/

Hello again,

I thought I included the specifics of my custom logger, but I did not
do that.
Please allow me to present this issue again with the complete
details.

--------------- myhandler.py -----------------------
import logging.handlers

# create my handler class
class MyHandler(logging.handlers.RotatingFileHandler):
    def __init__(self, fn):
        logging.handlers.RotatingFileHandler.__init__(self, fn,

maxBytes=10485760, backupCount=5)

# Register handler in the "logging.handlers" namespace
logging.handlers.MyHandler = MyHandler

--------------- myLogs.py -------------------------
import logging
import logging.handlers
from myhandler import MyHandler  #custom handler
import os

##### EXCLUDED THE DETAILS FOR THE CUSTOMIZED LOGGER
############################

#log file formatter
format = logging.Formatter("%(asctime)s %(levelname)s %(filename)s %
(lineno)d %(message)s")

# setup the logger instance
log = logging.getLogger("app_log")
# set the log level
log.setLevel(logging.DEBUG)
# add a method for the custom log level to the logger
setattr(log, 'userinfo', lambda *args: log.log(USERINFO, *args))

# create the handler for app.log
app_handler = logging.handlers.MyHandler(APP_LOG_FILENAME)    #using
myhandler
# set handler level
app_handler.setLevel(logging.DEBUG)
# add the formatter to the handler
app_handler.setFormatter(format)


# create the handler for notice.log
notice_handler =  logging.handlers.MyHandler(NOTICE_LOG_FILENAME)  #
using my handler
# set handler level
notice_handler.setLevel(logging.ERROR)
# add the formatter to the handler
notice_handler.setFormatter(format)


# setup the logger for user.log
userLog = logging.getLogger("user_log")
# set the level
userLog.setLevel(logging.INFO)
# add a method for the custom log level to the logger
setattr(userLog, 'userinfo', lambda *args: userLog.log(USERINFO,
*args))

# create the  handler for user.log
user_handler =  logging.handlers.MyHandler(USER_LOG_FILENAME)  # using
myhandler
# handler level
user_handler.setLevel(logging.INFO)
# add the formatter to the handler
user_handler.setFormatter(format)

# add the handlers to log
log.addHandler(app_handler)
log.addHandler(notice_handler)

# add the handler to userLog
userLog.addHandler(user_handler)

--------- app.py -----------------------------------

import logging
import myLogs


myLogs.log.debug('this is debug message')



###########################################################

I hope this provides much better clarification.



More information about the Python-list mailing list