Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ed6bb14

Browse files
committed
Copyright year & version number/version date changes.
Exception traceback text is now cached. Closing a handler now removes it from the internal _handlers list. Handlers now chain to Handler.close() from their close() methods. Exception info can be passed as a tuple in exc_info. shutdown() is registered to be called at application exit.
1 parent 48cfe38 commit ed6bb14

1 file changed

Lines changed: 36 additions & 8 deletions

File tree

Lib/logging/__init__.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
Should work under Python versions >= 1.5.2, except that source line
2222
information is not available unless 'sys._getframe()' is.
2323
24-
Copyright (C) 2001-2002 Vinay Sajip. All Rights Reserved.
24+
Copyright (C) 2001-2004 Vinay Sajip. All Rights Reserved.
2525
2626
To use, simply 'import logging' and log away!
2727
"""
@@ -36,8 +36,8 @@
3636

3737
__author__ = "Vinay Sajip <[email protected]>"
3838
__status__ = "beta"
39-
__version__ = "0.4.8.1"
40-
__date__ = "26 June 2003"
39+
__version__ = "0.4.9"
40+
__date__ = "20 February 2004"
4141

4242
#---------------------------------------------------------------------------
4343
# Miscellaneous module data
@@ -198,6 +198,7 @@ def __init__(self, name, level, pathname, lineno, msg, args, exc_info):
198198
self.filename = pathname
199199
self.module = "Unknown module"
200200
self.exc_info = exc_info
201+
self.exc_text = None # used to cache the traceback text
201202
self.lineno = lineno
202203
self.created = ct
203204
self.msecs = (ct - long(ct)) * 1000
@@ -364,9 +365,14 @@ def format(self, record):
364365
record.asctime = self.formatTime(record, self.datefmt)
365366
s = self._fmt % record.__dict__
366367
if record.exc_info:
368+
# Cache the traceback text to avoid converting it multiple times
369+
# (it's constant anyway)
370+
if not record.exc_text:
371+
record.exc_text = self.formatException(record.exc_info)
372+
if record.exc_text:
367373
if s[-1] != "\n":
368374
s = s + "\n"
369-
s = s + self.formatException(record.exc_info)
375+
s = s + record.exc_text
370376
return s
371377

372378
#
@@ -613,10 +619,17 @@ def close(self):
613619
"""
614620
Tidy up any resources used by the handler.
615621
616-
This version does nothing and is intended to be implemented by
617-
subclasses.
622+
This version does removes the handler from an internal list
623+
of handlers which is closed when shutdown() is called. Subclasses
624+
should ensure that this gets called from overridden close()
625+
methods.
618626
"""
619-
pass
627+
#get the module data lock, as we're updating a shared structure.
628+
_acquireLock()
629+
try: #unlikely to raise an exception, but you never know...
630+
del _handlers[self]
631+
finally:
632+
_releaseLock()
620633

621634
def handleError(self, record):
622635
"""
@@ -700,6 +713,7 @@ def close(self):
700713
Closes the stream.
701714
"""
702715
self.stream.close()
716+
StreamHandler.close(self)
703717

704718
#---------------------------------------------------------------------------
705719
# Manager classes and functions
@@ -989,7 +1003,8 @@ def _log(self, level, msg, args, exc_info=None):
9891003
else:
9901004
fn, lno = "<unknown file>", 0
9911005
if exc_info:
992-
exc_info = sys.exc_info()
1006+
if type(exc_info) != types.TupleType:
1007+
exc_info = sys.exc_info()
9931008
record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info)
9941009
self.handle(record)
9951010

@@ -1194,3 +1209,16 @@ def shutdown():
11941209
for h in _handlers.keys():
11951210
h.flush()
11961211
h.close()
1212+
1213+
#Let's try and shutdown automatically on application exit...
1214+
try:
1215+
import atexit
1216+
atexit.register(shutdown)
1217+
except ImportError: # for Python versions < 2.0
1218+
def exithook(status, old_exit=sys.exit):
1219+
try:
1220+
shutdown()
1221+
finally:
1222+
old_exit(status)
1223+
1224+
sys.exit = exithook

0 commit comments

Comments
 (0)