diff options
author | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2010-12-03 11:50:38 (GMT) |
---|---|---|
committer | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2010-12-03 11:50:38 (GMT) |
commit | 615615291f8a59ff194cadb67e9898575061aaa5 (patch) | |
tree | e0bca985218f63bb2ead7122fb512fcf9696df4b /Lib | |
parent | 97cbb76ee31b4b91fd908576bbc3354ceab41cfc (diff) | |
download | cpython-615615291f8a59ff194cadb67e9898575061aaa5.zip cpython-615615291f8a59ff194cadb67e9898575061aaa5.tar.gz cpython-615615291f8a59ff194cadb67e9898575061aaa5.tar.bz2 |
logging: Added getLogRecordFactory/setLogRecordFactory with docs and tests.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/logging/__init__.py | 29 | ||||
-rw-r--r-- | Lib/test/test_logging.py | 10 |
2 files changed, 21 insertions, 18 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index c4229e9..5256f95 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -33,7 +33,7 @@ __all__ = ['BASIC_FORMAT', 'BufferingFormatter', 'CRITICAL', 'DEBUG', 'ERROR', 'captureWarnings', 'critical', 'debug', 'disable', 'error', 'exception', 'fatal', 'getLevelName', 'getLogger', 'getLoggerClass', 'info', 'log', 'makeLogRecord', 'setLoggerClass', 'warn', 'warning', - 'getLogRecordClass', 'setLogRecordClass'] + 'getLogRecordFactory', 'setLogRecordFactory'] try: import codecs @@ -238,7 +238,7 @@ class LogRecord(object): information to be logged. """ def __init__(self, name, level, pathname, lineno, - msg, args, exc_info, func=None, sinfo=None): + msg, args, exc_info, func=None, sinfo=None, **kwargs): """ Initialize a logging record with interesting information. """ @@ -322,21 +322,24 @@ class LogRecord(object): # # Determine which class to use when instantiating log records. # -_logRecordClass = LogRecord +_logRecordFactory = LogRecord -def setLogRecordClass(cls): +def setLogRecordFactory(factory): """ Set the class to be used when instantiating a log record. + + :param factory: A callable which will be called to instantiate + a log record. """ - global _logRecordClass - _logRecordClass = cls + global _logRecordFactory + _logRecordFactory = factory -def getLogRecordClass(): +def getLogRecordFactory(): """ Return the class to be used when instantiating a log record. """ - return _logRecordClass + return _logRecordFactory def makeLogRecord(dict): """ @@ -345,7 +348,7 @@ def makeLogRecord(dict): a socket connection (which is sent as a dictionary) into a LogRecord instance. """ - rv = _logRecordClass(None, None, "", 0, "", (), None, None) + rv = _logRecordFactory(None, None, "", 0, "", (), None, None) rv.__dict__.update(dict) return rv @@ -1056,7 +1059,7 @@ class Manager(object): self.emittedNoHandlerWarning = 0 self.loggerDict = {} self.loggerClass = None - self.logRecordClass = None + self.logRecordFactory = None def getLogger(self, name): """ @@ -1100,12 +1103,12 @@ class Manager(object): + klass.__name__) self.loggerClass = klass - def setLogRecordClass(self, cls): + def setLogRecordFactory(self, factory): """ Set the class to be used when instantiating a log record with this Manager. """ - self.logRecordClass = cls + self.logRecordFactory = factory def _fixupParents(self, alogger): """ @@ -1305,7 +1308,7 @@ class Logger(Filterer): A factory method which can be overridden in subclasses to create specialized LogRecords. """ - rv = _logRecordClass(name, level, fn, lno, msg, args, exc_info, func, + rv = _logRecordFactory(name, level, fn, lno, msg, args, exc_info, func, sinfo) if extra is not None: for key in extra: diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 01c3336..cd8cdbd 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -1799,7 +1799,7 @@ class ChildLoggerTest(BaseTest): class DerivedLogRecord(logging.LogRecord): pass -class LogRecordClassTest(BaseTest): +class LogRecordFactoryTest(BaseTest): def setUp(self): class CheckingFilter(logging.Filter): @@ -1817,17 +1817,17 @@ class LogRecordClassTest(BaseTest): BaseTest.setUp(self) self.filter = CheckingFilter(DerivedLogRecord) self.root_logger.addFilter(self.filter) - self.orig_cls = logging.getLogRecordClass() + self.orig_factory = logging.getLogRecordFactory() def tearDown(self): self.root_logger.removeFilter(self.filter) BaseTest.tearDown(self) - logging.setLogRecordClass(self.orig_cls) + logging.setLogRecordFactory(self.orig_factory) def test_logrecord_class(self): self.assertRaises(TypeError, self.root_logger.warning, self.next_message()) - logging.setLogRecordClass(DerivedLogRecord) + logging.setLogRecordFactory(DerivedLogRecord) self.root_logger.error(self.next_message()) self.assert_log_lines([ ('root', 'ERROR', '2'), @@ -2015,7 +2015,7 @@ def test_main(): ConfigFileTest, SocketHandlerTest, MemoryTest, EncodingTest, WarningsTest, ConfigDictTest, ManagerTest, FormatterTest, - LogRecordClassTest, ChildLoggerTest, QueueHandlerTest, + LogRecordFactoryTest, ChildLoggerTest, QueueHandlerTest, RotatingFileHandlerTest, #TimedRotatingFileHandlerTest ) |