[issue45465] logging messages are needlessly reformatted for every handler

Роман Донченко report at bugs.python.org
Wed Oct 13 18:39:47 EDT 2021


New submission from Роман Донченко <dpb at corrigendum.ru>:

Consider this code:

```
import logging

class MyLogRecord(logging.LogRecord):
    def getMessage(self):
        print("Help! I am being formatted!")
        return super().getMessage()

logging.setLogRecordFactory(MyLogRecord)

logger = logging.getLogger("test")
logger.addHandler(logging.StreamHandler())
logger.addHandler(logging.StreamHandler())

logger.error("%d", 123)
```

Its output is:

```
Help! I am being formatted!
123
Help! I am being formatted!
123
```

In other words, the record's `getMessage` method is called once for every handler. That seems quite unnecessary, especially since the formatted message is saved in the `message` field of the record by `Formatter.format`. `Formatter` could check whether the message has already been formatted, and if so, use the saved message.

----------
components: Library (Lib)
messages: 403879
nosy: SpecLad
priority: normal
severity: normal
status: open
title: logging messages are needlessly reformatted for every handler
type: enhancement
versions: Python 3.10

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


More information about the Python-bugs-list mailing list