473,799 Members | 2,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hello,

I'm trying to understand the behavior of the Python 2.3 logging module (MS
Windows 2k) with regard to RotatingFileHan dler. 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\e xperimental\Py Logging\t_xb.py ", line 63, in ?
shutdown()
File "C:\Python23\li b\logging\__ini t__.py", line 1195, in shutdown
h.flush()
File "C:\Python23\li b\logging\__ini t__.py", line 661, in flush
self.stream.flu sh()
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.handler s import RotatingFileHan dler

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

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

global handler

logger = getLogger(compo nent)

if handler:
handler.flush()
handler.close()
logger.removeHa ndler(handler)

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

handler = RotatingFileHan dler(filename, mode, maxBytes, backupCount)

handler.setLeve l(DEBUG)

logger.addHandl er(handler)

return logger

handler = None

for i in range(20):
log = logger_for('sup plier')
log.error('test ing Python logging module')

shutdown()
END FILE _______________ _______________ ___________
Jul 18 '05 #1
5 11280
j vickroy wrote:
I'm trying to understand the behavior of the Python 2.3 logging module (MS
Windows 2k) with regard to RotatingFileHan dler. The following script
illustrates a puzzling problem. What is wrong with this script? if handler:
handler.flush()
handler.close()
logger.removeHa ndler(handler)


The handler is stored in the logging._handle rs dictionary in order to close
it when shutdown() is called. But you already did close it manually.
I think you have three options to fix your script.

(1) Don't call shutdown() at all and manually close the last handler
instead.

(2) Change the above to

if handler:
logger.removeHa ndler(handler)
handler.flush()
handler.close()
del logging._handle rs[handler]

so that shutdown() cannot touch closed handlers, or

(3) don't close handlers manually

if handler:
logger.removeHa ndler(handler)

so that shutdown() gets a still open handler as expected. I would go with
the latter, as it does not rely on implementation details.

(all untested, use at your own risk)

Peter

Jul 18 '05 #2

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

Traceback (most recent call last):
File
"C:\Python23\li b\site-packages\Python win\pywin\frame work\scriptutil s.py",
line 310, in RunScript
exec codeObject in __main__.__dict __
File "E:\$PROJECTS\e xperimental\Py Logging\t_xc.py ", line 61, in ?
log.error('test ing Python logging module')
File "C:\Python23\li b\logging\__ini t__.py", line 923, in error
apply(self._log , (ERROR, msg, args), kwargs)
File "C:\Python23\li b\logging\__ini t__.py", line 994, in _log
self.handle(rec ord)
File "C:\Python23\li b\logging\__ini t__.py", line 1004, in handle
self.callHandle rs(record)
File "C:\Python23\li b\logging\__ini t__.py", line 1037, in callHandlers
hdlr.handle(rec ord)
File "C:\Python23\li b\logging\__ini t__.py", line 592, in handle
self.emit(recor d)
File "C:\Python23\li b\logging\handl ers.py", line 105, in emit
self.doRollover ()
File "C:\Python23\li b\logging\handl ers.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.handler s import RotatingFileHan dler

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

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

global handler

logger = getLogger(compo nent)

if handler:
## handler.flush()
## handler.close()
logger.removeHa ndler(handler)

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

handler = RotatingFileHan dler(filename, mode, maxBytes, backupCount)

handler.setLeve l(DEBUG)

logger.addHandl er(handler)

return logger

handler = None

for i in range(20):
log = logger_for('sup plier')
log.error('test ing Python logging module')

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

Jul 18 '05 #3
As a follow-up question, why is a handle object not removed from
logging._handle rs when its (i.e., handle) close() procedure is applied?

That behavior appears to be responsible for the logging.shutdow n() failure.
"j vickroy" <ji*********@no aa.gov> wrote in message
news:bp******** **@boulder.noaa .gov...
Hello,

I'm trying to understand the behavior of the Python 2.3 logging module (MS
Windows 2k) with regard to RotatingFileHan dler. 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\e xperimental\Py Logging\t_xb.py ", line 63, in ?
shutdown()
File "C:\Python23\li b\logging\__ini t__.py", line 1195, in shutdown
h.flush()
File "C:\Python23\li b\logging\__ini t__.py", line 661, in flush
self.stream.flu sh()
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.handler s import RotatingFileHan dler

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

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

global handler

logger = getLogger(compo nent)

if handler:
handler.flush()
handler.close()
logger.removeHa ndler(handler)

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

handler = RotatingFileHan dler(filename, mode, maxBytes, backupCount)

handler.setLeve l(DEBUG)

logger.addHandl er(handler)

return logger

handler = None

for i in range(20):
log = logger_for('sup plier')
log.error('test ing Python logging module')

shutdown()
END FILE _______________ _______________ ___________

Jul 18 '05 #4
j vickroy wrote:

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

Traceback (most recent call last):
File
"C:\Python23\li b\site-packages\Python win\pywin\frame work\scriptutil s.py",
line 310, in RunScript
exec codeObject in __main__.__dict __
File "E:\$PROJECTS\e xperimental\Py Logging\t_xc.py ", line 61, in ?
log.error('test ing Python logging module')
File "C:\Python23\li b\logging\__ini t__.py", line 923, in error
apply(self._log , (ERROR, msg, args), kwargs)
File "C:\Python23\li b\logging\__ini t__.py", line 994, in _log
self.handle(rec ord)
File "C:\Python23\li b\logging\__ini t__.py", line 1004, in handle
self.callHandle rs(record)
File "C:\Python23\li b\logging\__ini t__.py", line 1037, in callHandlers
hdlr.handle(rec ord)
File "C:\Python23\li b\logging\__ini t__.py", line 592, in handle
self.emit(recor d)
File "C:\Python23\li b\logging\handl ers.py", line 105, in emit
self.doRollover ()
File "C:\Python23\li b\logging\handl ers.py", line 90, in doRollover
os.rename(self. baseFilename, dfn)
OSError: [Errno 13] Permission denied


Works here. Consider switching to an OS where you can rename an open file...
Well, you read my discalimer :-)

Peter
Jul 18 '05 #5
j vickroy wrote:
As a follow-up question, why is a handle object not removed from
logging._handle rs when its (i.e., handle) close() procedure is applied?

That behavior appears to be responsible for the logging.shutdow n()
failure.


Seems that the author did not consider the use case of consecutively using
different handlers operating on the same file set - I've not yet made up my
mind, if you are misusing the logging system or if that's a bug.

Anyway, as of 2.3.2 the package has still __status__ = "beta", so patches
might be welcome.

Peter
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1423
by: max | last post by:
I have an application which calls a module runx, in runx I import logging and get a logger add handlers. I want to be able to rename the file created by fileHandler. several problems, I can't seem to be able to rename the file, calling shutdown, or removeHandler and then hndlr.close(), I get No handlers could be found for logger "root" even though I am not calling any related methods. The second problem I find if I re-enter my module...
0
1680
by: Neil Benn | last post by:
Hello, I'm running a test and having issues with logging, if I call logging.shutdown() and then want to start the logging going again then I get a problem as if I call shutdown, I can't get the root logger again, such as : ..>>> import logging ..>>> objTestLogger = logging.getLogger() ..>>> objTestLogger.setLevel(logging.INFO)
2
3330
by: flupke | last post by:
Hi, i have a class and a class attribute log which is a logger object. In the __del__() function i want to log a message but it fails even if i use self.__class__.log. The error i get is this: Traceback (most recent call last): File "C:\Python24\lib\logging\__init__.py", line 712, in emit self.stream.write(fs % msg)
1
2371
by: Oliver Eichler | last post by:
Hi, I experience several exceptions from python's logging system when using the rollover feature on Windows. Traceback (most recent call last): File "c:\Python24\lib\logging\handlers.py", line 62, in emit if self.shouldRollover(record): File "c:\Python24\lib\logging\handlers.py", line 132, in shouldRollover self.stream.seek(0, 2) #due to non-posix-compliant Windows feature
1
1970
by: usenet | last post by:
I'm having some problems getting the logging module to work with the threading module. I've narrowed the problem down to the following code: import logging, threading update_log = logging.getLogger('update_log') update_log.addHandler(logging.FileHandler("/tmp/update_log")) class dlThread(threading.Thread):
7
8591
by: flupke | last post by:
Hi, i'm getting errors with the log module concerning RotatingFileHandler. I'm using Python 2.4.3 on Windows XP SP2. This used to work in previous python versions but since i upgraded to 2.4.3 i get these errors: Traceback (most recent call last): File "C:\Python24\lib\logging\handlers.py", line 71, in emit if self.shouldRollover(record):
1
385
by: s99999999s2003 | last post by:
hi i have defined a function def logger(logfile,msg): import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='%s' % (logfile),
3
5411
by: nicholas.petrella | last post by:
I am currently trying to use the python logging system as a core enterprise level logging solution for our development and production environments. The rotating file handler seems to be what I am looking for as I want the ability to have control over the number and size of log files that are written out for each of our tools. I have noticed a few problems with this handler and wanted to post here to get your impressions and possibly...
2
4090
by: scriptlearner | last post by:
OS: Solaris 9 Python Version: 2.4.4 I need to log certain data in a worker thread; however, I am getting an error now when I use two worker threads. I think the problem comes from the line logging.info('Thread Object (%d):(%d), Time:%s in seconds %d'% (self.no,self.duration,time.ctime(),time.time())) when multiple worker thread is trying to update the log files. What did I do wrong? Should I lock the log file before writing to
0
9546
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10268
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10031
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.