473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

logging error with RotatingFileHandler

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):
File "C:\Python24\lib\logging\handlers.py", line 150, in shouldRollover
self.stream.seek(0, 2) #due to non-posix-compliant Windows feature
ValueError: I/O operation on closed file
2006-06-25 10:35:07,171 INFO firebird 210 Starting up the database
Traceback (most recent call last):
File "C:\Python24\lib\logging\handlers.py", line 72, in emit
self.doRollover()
File "C:\Python24\lib\logging\handlers.py", line 134, in doRollover
self.handleError(record)
NameError: global name 'record' is not defined

I use the logging module by loading a config file:
import logging
import logging.config
LOGFILE = r"logconf.ini"
Then in the init function of the class
# load logger
logging.config.fileConfig(LOGFILE)
self.log = logging.getLogger('stats')

If i have a main class and a class deriving from it, i let the main
class instantiate the class by issueing the commands like above and then
get automatically get access to it in the deriving classes.

If i remove the logfile, it all works until it has to rollover.

My config.ini file contains this info:

[loggers]
keys=root,stats,database

[handlers]
keys=hand01,hand02

[formatters]
keys=form01,form02

[logger_root]
level=NOTSET
handlers=hand01
qualname=(root) # note - this is used in non-root loggers
propagate=1 # note - this is used in non-root loggers
channel=
parent=

[logger_stats]
level=DEBUG
propagate=0
qualname=stats
handlers=hand01,hand02
channel=log02
parent=(root)

[logger_database]
level=DEBUG
propagate=0
qualname=database
handlers=hand01,hand02
channel=log03
parent=(root2

[handler_hand01]
class=StreamHandler
level=DEBUG
formatter=form01
args=(sys.stdout,)
stream=sys.stdout

[handler_hand02]
class=handlers.RotatingFileHandler
level=NOTSET
formatter=form01
filename=stats.log
mode=a
maxsize=500000
backcount=9
args=('stats.log', 'a', 900000, 5)

[formatter_form01]
format=%(asctime)s %(levelname)s %(module)s %(lineno)d %(message)s
datefmt=

[formatter_form02]
format=%(asctime)s %(levelname)s %(module)s %(lineno)d %(message)s
datefmt=

Any idea on why this suddenly doesn't work anymore?
Thanks
Jun 25 '06 #1
7 8515
flupke wrote:
File "C:\Python24\lib\logging\handlers.py", line 134, in doRollover
self.handleError(record)
NameError: global name 'record' is not defined


There's a bug in doRollover's exception handling, which is masking the
true error - which is most probably an exception being thrown in
os.rename.

I'll look at fixing this bug asap. Most likely, it'll be done by let
the exception propagate up from doRollover to its caller.

Regards,

Vinay Sajip

Jun 27 '06 #2
Vinay Sajip wrote:
flupke wrote:

File "C:\Python24\lib\logging\handlers.py", line 134, in doRollover
self.handleError(record)
NameError: global name 'record' is not defined

There's a bug in doRollover's exception handling, which is masking the
true error - which is most probably an exception being thrown in
os.rename.

I'll look at fixing this bug asap. Most likely, it'll be done by let
the exception propagate up from doRollover to its caller.

Regards,

Vinay Sajip


OK Vinjay,

thanks for the info.
If this bug is fixed, where will i find a fixed version and how do i
install it?
Also, more important to me, what am i doing wrong to get this error
message in the first place?

I could reproduce the error with this program:

import logging
import logging.config

LOGFILE = r"logconf.ini"
logging.config.fileConfig(LOGFILE)

class TypeA(object):
def __init__(self):
#logging.config.fileConfig(LOGFILE)
self.log = logging.getLogger('logfile')
self.dolog("Starting the program")

def dolog(self,text):
self.log.debug(text)

class TypeB(TypeA):
def __init__(self):
super(TypeB,self).__init__()
# put this line in comment to prevent the error
logging.config.fileConfig(LOGFILE)
#self.log = logging.getLogger('logfile')
self.log.debug("Starting class B")

for nr_of_lognr in range(0,2000):
b = TypeB()
b.dolog("This is a test to see if logging works and more
specifically the RotatingFileHandler functionallity")

It seemed as if the "logging.config.fileConfig(LOGFILE)" line in Type B
is to blame. I changed the code of my big program to reflect this change
yet i still get this error.

I didn't get this with an earlier version of python.

Regards,
Benedict
Jun 28 '06 #3
flupke wrote:
If this bug is fixed, where will i find a fixed version and how do i
install it?
Also, more important to me, what am i doing wrong to get this error
message in the first place?


You're not doing anything particularly wrong, though it's not exactly
good practice to call fileConfig lots of times. (It's really meant for
one-off configuration, not incremental configuration.)

I get an error with just the following script typed in at the
interactive prompt:

import logging.config
logging.config.fileConfig("logconf.ini")
logging.config.fileConfig("logconf.ini")

So, I will investigate, and when I check in a fix to the Python
Subversion repository, I will post a message to this thread.

Regards,

Vinay Sajip

Jun 29 '06 #4
Hi,

I have found some inaccuracy in the Python Library Reference
6.29.5.4 TimedRotatingFileHandler
(http://docs.python.org/lib/node349.html)

1)
it is not really obvious from the documentation the "when" value has
to be in the format "w0", "w1" ... "w6" if "Type of interval" is
defined as a "week day"

2)
next the documentation says:
"If backupCount is non-zero, the system will save old log files by
appending the extensions ".1", ".2" etc., to the filename"

This is not really true, in this case.
The extension is (depending on the "when" value): "timestamp" or
"date+hour+minutes" or "date+hours" or "date".

Regards
Petr Jakes

Jun 29 '06 #5
Vinay Sajip schreef:
flupke wrote:
If this bug is fixed, where will i find a fixed version and how do i
install it?
Also, more important to me, what am i doing wrong to get this error
message in the first place?


You're not doing anything particularly wrong, though it's not exactly
good practice to call fileConfig lots of times. (It's really meant for
one-off configuration, not incremental configuration.)

I get an error with just the following script typed in at the
interactive prompt:

import logging.config
logging.config.fileConfig("logconf.ini")
logging.config.fileConfig("logconf.ini")

So, I will investigate, and when I check in a fix to the Python
Subversion repository, I will post a message to this thread.

Regards,

Vinay Sajip


Hi Vinay,

thanks for the info.

I tried to change the handlers.py file.
First, i changed some code in doRollover.

if os.path.exists(dfn):
os.remove(dfn)
try:
#os.rename(self.baseFilename, dfn)

-> The rename fails for some reason.

I tried with the move function of shutil
shutil.move(self.baseFilename, dfn)

This partially works: the file is now rotated but i still get an error:
The error that i get is this:
Error [Errno 13] Permission denied: 'E:\\python\\proj1\\src\\proj1.log'
Traceback (most recent call last):
File "C:\Python24\lib\logging\handlers.py", line 72, in emit
self.doRollover()
File "C:\Python24\lib\logging\handlers.py", line 141, in doRollover
self.handleError(record)
NameError: global name 'record' is not defined

The last part of the error shows that the part where the exception is
caugth is also not working correctly as the "record" isn't know.

When i close my program, i get this error as well:

Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "C:\Python24\lib\atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "C:\Python24\lib\logging\__init__.py", line 1333, in shutdown
h.close()
File "C:\Python24\lib\logging\__init__.py", line 772, in close
StreamHandler.close(self)
File "C:\Python24\lib\logging\__init__.py", line 674, in close
del _handlers[self]
KeyError: <logging.handlers.RotatingFileHandler instance at 0x01E098A0>
Error in sys.exitfunc:
Traceback (most recent call last):
File "C:\Python24\lib\atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "C:\Python24\lib\logging\__init__.py", line 1333, in shutdown
h.close()
File "C:\Python24\lib\logging\__init__.py", line 772, in close
StreamHandler.close(self)
File "C:\Python24\lib\logging\__init__.py", line 674, in close
del _handlers[self]
KeyError: <logging.handlers.RotatingFileHandler instance at 0x01E098A0>

Regards
Benedict
Jun 30 '06 #6
flupke schreef:
<snip>
Hi Vinay,

thanks for the info.

I tried to change the handlers.py file.
First, i changed some code in doRollover.

if os.path.exists(dfn):
os.remove(dfn)
try:
#os.rename(self.baseFilename, dfn)

-> The rename fails for some reason.

I tried with the move function of shutil
shutil.move(self.baseFilename, dfn)

This partially works: the file is now rotated but i still get an error:
The error that i get is this:
Error [Errno 13] Permission denied: 'E:\\python\\proj1\\src\\proj1.log'
Traceback (most recent call last):
File "C:\Python24\lib\logging\handlers.py", line 72, in emit
self.doRollover()
File "C:\Python24\lib\logging\handlers.py", line 141, in doRollover
self.handleError(record)
NameError: global name 'record' is not defined

The last part of the error shows that the part where the exception is
caugth is also not working correctly as the "record" isn't know.

When i close my program, i get this error as well:

Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "C:\Python24\lib\atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "C:\Python24\lib\logging\__init__.py", line 1333, in shutdown
h.close()
File "C:\Python24\lib\logging\__init__.py", line 772, in close
StreamHandler.close(self)
File "C:\Python24\lib\logging\__init__.py", line 674, in close
del _handlers[self]
KeyError: <logging.handlers.RotatingFileHandler instance at 0x01E098A0>
Error in sys.exitfunc:
Traceback (most recent call last):
File "C:\Python24\lib\atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "C:\Python24\lib\logging\__init__.py", line 1333, in shutdown
h.close()
File "C:\Python24\lib\logging\__init__.py", line 772, in close
StreamHandler.close(self)
File "C:\Python24\lib\logging\__init__.py", line 674, in close
del _handlers[self]
KeyError: <logging.handlers.RotatingFileHandler instance at 0x01E098A0>

Regards
Benedict


I was able to solve the errors (i think):

1. in the function doRollover of the class RotatingFileHandler

if os.path.exists(dfn):
os.remove(dfn)
try:
os.rename(self.baseFilename, dfn)

I added this instead of the rename function:
import shutil
shutil.copy(self.baseFilename, dfn)

If this code works ok, then the "import shutil" should be placed at the
top of the file for performance reasons.
The logic of the copy is that a few statements later, the self.stream
var is set to a new file again so it doesn't need to be deleted. At
least it seems to work here. I will need to test further.
Weird thing is, the stream is closed and yet you get this error when you
use rename:
[Errno 13] Permission denied

2. in the same function, when an error occurred, the record var wasn't
found.
This can be solved by changing the code of emit in BaseRotatingHandler.
Change this:
if self.shouldRollover(record):
self.doRollover()
by:
if self.shouldRollover(record):
self.doRollover(record)

3. The last error (error when closing the program) can be solved by
editing the __init__.py, close(self) function.
Change this:
_acquireLock()
try: #unlikely to raise an exception, but you never know...
del _handlers[self]
_handlerList.remove(self)
finally:
_releaseLock()

by:
_acquireLock()
try: #unlikely to raise an exception, but you never know...
#del _handlers[self]
if ( _handlers.has_key(self) ): del _handlers[self]
#if ( self in _handlerList ): _handlerList.remove(self)
_handlerList.remove(self)
finally:
_releaseLock()

It might even be better to change the _handlerList.remove(self)
statement by this statement:
if ( self in _handlerList ): _handlerList.remove(self)

Regards,
Benedict Verheyen
Jun 30 '06 #7

flupke wrote:
-The rename fails for some reason.
I would dig a little deeper to find out what the reason is. After all,
on the face of it, there's no obvious reason it should fail. The
"permission denied" indicates perhaps that some other process or thread
is keeping that file open?

I've fixed (AFAICT) both the crash at shutdown and the buggy exception
handling, in the Python subversion repository. You can download the
latest from here and try it.

http://svn.python.org/view/python/trunk/Lib/logging/

Regards,

Vinay Sajip

Jul 2 '06 #8

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

Similar topics

5
by: j vickroy | last post by:
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...
0
by: Logan | last post by:
Is there actually a good reason why the StreamHandler and the FileHandler are in the core package and all other handlers are in logging.handlers? I first did not realize this when reading the...
0
by: Kamus of Kadizhar | last post by:
Thanks to Robert Brewer, I got enough insight into logging to make it work.... Now I have another issue: file locking. Sorry if this is a very basic question, but I can't find a handy reference...
12
by: Rob Cranfill | last post by:
Hello, I've successfully coded Python to do what I want with a RotatingFileHandler, but am having trouble getting the same behavior via a config file. I wanted to create one log file each...
3
by: seb | last post by:
Hi, I am writing to a file some basic information using the logging module. It is working but in the log file some line are printed several time. I had put some print debugging messages in the...
3
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...
3
by: Chris Shenton | last post by:
I am setting up handlers to log DEBUG and above to a rotating file and ERROR and above to console. But if any of my code calls a logger (e.g., logging.error("foo")) before I setup my handlers, the...
1
by: Kenneth Love | last post by:
I have a Python logging config file that contains a RotatingFileHandler handler. In the args key, I have hard-coded the log filename. Everything works great. However, I find that I now need to...
3
by: Lowell Alleman | last post by:
Here is the situation: I wrote my own log handler class (derived from logging.Handler) and I want to be able to use it from a logging config file, that is, a config file loaded with the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.