Made logging classes new-style and added name property to handlers.

This commit is contained in:
Vinay Sajip 2009-11-24 15:53:25 +00:00
parent 3e5354934e
commit 1d2c16db7e
1 changed files with 33 additions and 16 deletions

View File

@ -46,8 +46,8 @@ except ImportError:
__author__ = "Vinay Sajip <vinay_sajip@red-dove.com>" __author__ = "Vinay Sajip <vinay_sajip@red-dove.com>"
__status__ = "production" __status__ = "production"
__version__ = "0.5.0.9" __version__ = "0.5.1.0"
__date__ = "09 October 2009" __date__ = "24 November 2009"
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
# Miscellaneous module data # Miscellaneous module data
@ -196,9 +196,9 @@ def _checkLevel(level):
# #
#_lock is used to serialize access to shared data structures in this module. #_lock is used to serialize access to shared data structures in this module.
#This needs to be an RLock because fileConfig() creates Handlers and so #This needs to be an RLock because fileConfig() creates and configures
#might arbitrary user threads. Since Handler.__init__() updates the shared #Handlers, and so might arbitrary user threads. Since Handler code updates the
#dictionary _handlers, it needs to acquire the lock. But if configuring, #shared dictionary _handlers, it needs to acquire the lock. But if configuring,
#the lock would already have been acquired - so we need an RLock. #the lock would already have been acquired - so we need an RLock.
#The same argument applies to Loggers and Manager.loggerDict. #The same argument applies to Loggers and Manager.loggerDict.
# #
@ -227,7 +227,7 @@ def _releaseLock():
# The logging record # The logging record
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
class LogRecord: class LogRecord(object):
""" """
A LogRecord instance represents an event being logged. A LogRecord instance represents an event being logged.
@ -335,7 +335,7 @@ def makeLogRecord(dict):
# Formatter classes and functions # Formatter classes and functions
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
class Formatter: class Formatter(object):
""" """
Formatter instances are used to convert a LogRecord to text. Formatter instances are used to convert a LogRecord to text.
@ -467,7 +467,7 @@ class Formatter:
# #
_defaultFormatter = Formatter() _defaultFormatter = Formatter()
class BufferingFormatter: class BufferingFormatter(object):
""" """
A formatter suitable for formatting a number of records. A formatter suitable for formatting a number of records.
""" """
@ -509,7 +509,7 @@ class BufferingFormatter:
# Filter classes and functions # Filter classes and functions
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
class Filter: class Filter(object):
""" """
Filter instances are used to perform arbitrary filtering of LogRecords. Filter instances are used to perform arbitrary filtering of LogRecords.
@ -546,7 +546,7 @@ class Filter:
return 0 return 0
return (record.name[self.nlen] == ".") return (record.name[self.nlen] == ".")
class Filterer: class Filterer(object):
""" """
A base class for loggers and handlers which allows them to share A base class for loggers and handlers which allows them to share
common code. common code.
@ -590,7 +590,7 @@ class Filterer:
# Handler classes and functions # Handler classes and functions
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
_handlers = {} #repository of handlers (for flushing when shutdown called) _handlers = {} #map of handler names to handlers
_handlerList = [] # added to allow handlers to be removed in reverse of order initialized _handlerList = [] # added to allow handlers to be removed in reverse of order initialized
class Handler(Filterer): class Handler(Filterer):
@ -608,17 +608,33 @@ class Handler(Filterer):
and the filter list to empty. and the filter list to empty.
""" """
Filterer.__init__(self) Filterer.__init__(self)
self._name = None
self.level = _checkLevel(level) self.level = _checkLevel(level)
self.formatter = None self.formatter = None
#get the module data lock, as we're updating a shared structure. #get the module data lock, as we're updating a shared structure.
_acquireLock() _acquireLock()
try: #unlikely to raise an exception, but you never know... try: #unlikely to raise an exception, but you never know...
_handlers[self] = 1
_handlerList.insert(0, self) _handlerList.insert(0, self)
finally: finally:
_releaseLock() _releaseLock()
self.createLock() self.createLock()
def get_name(self):
return self._name
def set_name(self, name):
_acquireLock()
try:
if self._name in _handlers:
del _handlers[self._name]
self._name = name
if name:
_handlers[name] = self
finally:
_releaseLock()
name = property(get_name, set_name)
def createLock(self): def createLock(self):
""" """
Acquire a thread lock for serializing access to the underlying I/O. Acquire a thread lock for serializing access to the underlying I/O.
@ -716,7 +732,8 @@ class Handler(Filterer):
#get the module data lock, as we're updating a shared structure. #get the module data lock, as we're updating a shared structure.
_acquireLock() _acquireLock()
try: #unlikely to raise an exception, but you never know... try: #unlikely to raise an exception, but you never know...
del _handlers[self] if self._name and self._name in _handlers:
del _handlers[self._name]
_handlerList.remove(self) _handlerList.remove(self)
finally: finally:
_releaseLock() _releaseLock()
@ -869,7 +886,7 @@ class FileHandler(StreamHandler):
# Manager classes and functions # Manager classes and functions
#--------------------------------------------------------------------------- #---------------------------------------------------------------------------
class PlaceHolder: class PlaceHolder(object):
""" """
PlaceHolder instances are used in the Manager logger hierarchy to take PlaceHolder instances are used in the Manager logger hierarchy to take
the place of nodes for which no loggers have been defined. This class is the place of nodes for which no loggers have been defined. This class is
@ -916,7 +933,7 @@ def getLoggerClass():
return _loggerClass return _loggerClass
class Manager: class Manager(object):
""" """
There is [under normal circumstances] just one Manager instance, which There is [under normal circumstances] just one Manager instance, which
holds the hierarchy of loggers. holds the hierarchy of loggers.
@ -1269,7 +1286,7 @@ class RootLogger(Logger):
_loggerClass = Logger _loggerClass = Logger
class LoggerAdapter: class LoggerAdapter(object):
""" """
An adapter for loggers which makes it easier to specify contextual An adapter for loggers which makes it easier to specify contextual
information in logging output. information in logging output.