logging: Added getLogRecordFactory/setLogRecordFactory with docs and tests.
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 @@
'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 @@
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 @@
#
# 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.
- """
- global _logRecordClass
- _logRecordClass = cls
-def getLogRecordClass():
+ :param factory: A callable which will be called to instantiate
+ a log record.
+ """
+ global _logRecordFactory
+ _logRecordFactory = factory
+
+def getLogRecordFactory():
"""
Return the class to be used when instantiating a log record.
"""
- return _logRecordClass
+ return _logRecordFactory
def makeLogRecord(dict):
"""
@@ -345,7 +348,7 @@
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 @@
self.emittedNoHandlerWarning = 0
self.loggerDict = {}
self.loggerClass = None
- self.logRecordClass = None
+ self.logRecordFactory = None
def getLogger(self, name):
"""
@@ -1100,12 +1103,12 @@
+ 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 @@
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 DerivedLogRecord(logging.LogRecord):
pass
-class LogRecordClassTest(BaseTest):
+class LogRecordFactoryTest(BaseTest):
def setUp(self):
class CheckingFilter(logging.Filter):
@@ -1817,17 +1817,17 @@
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 @@
ConfigFileTest, SocketHandlerTest, MemoryTest,
EncodingTest, WarningsTest, ConfigDictTest, ManagerTest,
FormatterTest,
- LogRecordClassTest, ChildLoggerTest, QueueHandlerTest,
+ LogRecordFactoryTest, ChildLoggerTest, QueueHandlerTest,
RotatingFileHandlerTest,
#TimedRotatingFileHandlerTest
)