This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author penlect
Recipients penlect, vinay.sajip
Date 2019-11-12.21:43:12
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1573594992.8.0.415095728854.issue38781@roundup.psfhosted.org>
In-reply-to
Content
The `logging.handlers.MemoryHandler` has a method `flush` which clears the buffer by assigning an empty list literal:

self.buffer = []

This forces the buffer to be a list instance.
My suggestion is to clear the buffer like this instead:

self.buffer.clear()

In this way it would be possible to implement a custom buffer or use the `collections.deque` when subclassing `MemoryHandler`. At the moment you must copy-past the `flush` method and modify it accordingly in the subclass:

```
def flush(self):
    # (Docstring skipped)
    self.acquire()
    try:
        if self.target:
	    for record in self.buffer:
	        self.target.handle(record)
	    self.buffer = []  # <-- change to `self.buffer.clear()`
    finally:
        self.release()
```

Example where this change is useful
===================================
This example creates a DequeMemoryHandler which uses the `collections.deque` as a buffer. Only the latest `capacity` number of records will stored in the buffer. The buffer is then flushed if and only if a record has a level greater or equal to `logging.ERROR`.

```
import collections
import logging
from logging.handlers import MemoryHandler


class DequeMemoryHandler(MemoryHandler):

    def __init__(self, capacity, *args, **kwargs):
        super().__init__(capacity, *args, **kwargs)
        self.buffer = collections.deque(maxlen=capacity)

    def shouldFlush(self, record):
        return record.levelno >= self.flushLevel


handler = DequeMemoryHandler(capacity=5, target=logging.StreamHandler())
logging.basicConfig(level=logging.INFO, handlers=[handler])

for i in range(1, 21):
    logging.info('Spam %d', i)
    if i % 10 == 0:
        logging.error(f'---> Eggs {i}')
```


Desired output (with the change):
---------------------------------
Spam 7
Spam 8
Spam 9
Spam 10
---> Eggs 10
Spam 17
Spam 18
Spam 19
Spam 20
---> Eggs 20


Actual output (without the change):
-----------------------------------
Spam 7
Spam 8
Spam 9
Spam 10
---> Eggs 10
Spam 11
Spam 12
Spam 13
Spam 14
Spam 15
Spam 16
Spam 17
Spam 18
Spam 19
Spam 20
---> Eggs 20
History
Date User Action Args
2019-11-12 21:43:12penlectsetrecipients: + penlect, vinay.sajip
2019-11-12 21:43:12penlectsetmessageid: <1573594992.8.0.415095728854.issue38781@roundup.psfhosted.org>
2019-11-12 21:43:12penlectlinkissue38781 messages
2019-11-12 21:43:12penlectcreate