From 1d2c16db7ee7cd14a98fef4b3ec689be861bdcf0 Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Tue, 24 Nov 2009 15:53:25 +0000 Subject: [PATCH] Made logging classes new-style and added name property to handlers. --- Lib/logging/__init__.py | 49 +++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index f85296d8665..5b8cb4ec974 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -46,8 +46,8 @@ except ImportError: __author__ = "Vinay Sajip " __status__ = "production" -__version__ = "0.5.0.9" -__date__ = "09 October 2009" +__version__ = "0.5.1.0" +__date__ = "24 November 2009" #--------------------------------------------------------------------------- # Miscellaneous module data @@ -196,9 +196,9 @@ def _checkLevel(level): # #_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 -#might arbitrary user threads. Since Handler.__init__() updates the shared -#dictionary _handlers, it needs to acquire the lock. But if configuring, +#This needs to be an RLock because fileConfig() creates and configures +#Handlers, and so might arbitrary user threads. Since Handler code updates the +#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 same argument applies to Loggers and Manager.loggerDict. # @@ -227,7 +227,7 @@ def _releaseLock(): # The logging record #--------------------------------------------------------------------------- -class LogRecord: +class LogRecord(object): """ A LogRecord instance represents an event being logged. @@ -335,7 +335,7 @@ def makeLogRecord(dict): # Formatter classes and functions #--------------------------------------------------------------------------- -class Formatter: +class Formatter(object): """ Formatter instances are used to convert a LogRecord to text. @@ -467,7 +467,7 @@ class Formatter: # _defaultFormatter = Formatter() -class BufferingFormatter: +class BufferingFormatter(object): """ A formatter suitable for formatting a number of records. """ @@ -509,7 +509,7 @@ class BufferingFormatter: # Filter classes and functions #--------------------------------------------------------------------------- -class Filter: +class Filter(object): """ Filter instances are used to perform arbitrary filtering of LogRecords. @@ -546,7 +546,7 @@ class Filter: return 0 return (record.name[self.nlen] == ".") -class Filterer: +class Filterer(object): """ A base class for loggers and handlers which allows them to share common code. @@ -590,7 +590,7 @@ class Filterer: # 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 class Handler(Filterer): @@ -608,17 +608,33 @@ class Handler(Filterer): and the filter list to empty. """ Filterer.__init__(self) + self._name = None self.level = _checkLevel(level) self.formatter = None #get the module data lock, as we're updating a shared structure. _acquireLock() try: #unlikely to raise an exception, but you never know... - _handlers[self] = 1 _handlerList.insert(0, self) finally: _releaseLock() 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): """ 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. _acquireLock() 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) finally: _releaseLock() @@ -869,7 +886,7 @@ class FileHandler(StreamHandler): # Manager classes and functions #--------------------------------------------------------------------------- -class PlaceHolder: +class PlaceHolder(object): """ 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 @@ -916,7 +933,7 @@ def getLoggerClass(): return _loggerClass -class Manager: +class Manager(object): """ There is [under normal circumstances] just one Manager instance, which holds the hierarchy of loggers. @@ -1269,7 +1286,7 @@ class RootLogger(Logger): _loggerClass = Logger -class LoggerAdapter: +class LoggerAdapter(object): """ An adapter for loggers which makes it easier to specify contextual information in logging output.