ValueError: I/O operation on closed file

D

dj

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

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


I am completely stumped has to what could be the issue.
Any help would be greatly appreciated.
 
V

Vinay Sajip

I have a handler which I use with a set of log levels for the pythonloggingmodule.

----------------------------------- myhandler.py
--------------------------------------------------------
importlogging.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" namespacelogging.handlers.MyHandler = MyHandler
--------------------------------------------------------------------------------------------------------------------

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

I am completely stumped has to what could be the issue.
Any help would be greatly appreciated.

Not sure - you may need to post a small script which demonstrates the
error repeatably. Otherwise, it may be caused by e.g. other handles
being open to the same file (say, if you open a FileHandler and a
RotatingFileHandler with the same path, or have one of the log files
open in an editor which locks the file.

Regards,

Vinay Sajip
 
D

Dennis Lee Bieber

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
D

dj

        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?




        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
        (e-mail address removed)             (e-mail address removed)
                HTTP://wlfraed.home.netcom.com/
        (Bestiaria Support Staff:               (e-mail address removed))
                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.
 
D

Dennis Lee Bieber

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.

Posting the minimal code that reproduces the problem would be
better... but...

<snip>


After cleaning up the wrapped lines, and supplying the data for the
missing CONSTANT items...

The code works on my work installation (v2.4).

Hmm, just tried the files I'd emailed home... Works on v2.5...

-=-=-=-=-=-=-
E:\>python app.py
importing myLogs
myLogs importing logging
handler for app.log initialized
handler for notice.log initialized
handler for user.log initialized

E:\>type *.log

app.log


2009-04-13 22:33:42,717 DEBUG app.py 4 this is debug message
2009-04-13 22:33:42,717 Level 200 mylogs.py 22 What the?

notice.log


2009-04-13 22:33:42,717 Level 200 mylogs.py 22 What the?

E:\>
-=-=-=-=-=-=-
print "importing myLogs"
import myLogs

myLogs.log.debug('this is debug message')
myLogs.log.userinfo("What the?")
-=-=-=-=-=-=-
import logging.handlers

# create my handler class
class MyHandler(logging.handlers.RotatingFileHandler):
def __init__(self, fn):
print "handler for %s initialized" % fn
logging.handlers.RotatingFileHandler.__init__(self,
fn,
maxBytes=10485760,
backupCount=5)

# Register handler in the "logging.handlers" namespace
logging.handlers.MyHandler = MyHandler
-=-=-=-=-=-=-
print "myLogs importing logging"
import logging
##import logging.handlers
import myhandler
import os

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

USERINFO = 200
APP_LOG_FILENAME = "app.log"
NOTICE_LOG_FILENAME = "notice.log"
USER_LOG_FILENAME = "user.log"

#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)
-=-=-=-=-=-

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
V

Vinay Sajip

And Dennis' revised script works for me on Python 2.6 and [after
converting print x to print(x)] Python 3.0 (both ActivePython
installations).

Regards,

Vinay Sajip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top