[issue33057] logging.Manager.logRecordFactory is never used

Ben Feinstein report at bugs.python.org
Wed Mar 14 14:18:57 EDT 2018


Ben Feinstein <feinsteinben at gmail.com> added the comment:

Here is a code that demonstrate the bug:

```python
import logging

class LogRecordTypeFilter(logging.Filter):
    def __init__(self, cls):
        self.cls = cls

    def filter(self, record):
        t = type(record)
        if t is not self.cls:
            msg = 'Unexpected LogRecord type %s, expected %s' % (t, self.cls)
            raise TypeError(msg)
        return True

class MyLogRecord(logging.LogRecord):
    pass

manager = logging.Manager(None)
manager.setLogRecordFactory(MyLogRecord)
logger = manager.getLogger('some_logger')
logger.addFilter(LogRecordTypeFilter(MyLogRecord))

try:
    logger.error('bpo-33057')
except TypeError as e:
    print(e)  # output: Unexpected LogRecord type <class 'logging.LogRecord'>, expected <class '__main__.MyLogRecord'>
```

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33057>
_______________________________________


More information about the Python-bugs-list mailing list