-
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathhandler.py
More file actions
20 lines (15 loc) · 665 Bytes
/
handler.py
File metadata and controls
20 lines (15 loc) · 665 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import logging
from typing import List
class FormattedMessageCollectorHandler(logging.Handler):
"""A logging handler that stores formatted log records in its "messages" attribute."""
def __init__(self, level=logging.NOTSET) -> None:
"""Create a new log handler."""
super().__init__(level=level)
self.level = level
self.messages: list[str] = []
def emit(self, record: logging.LogRecord) -> None:
"""Keep the log records in a list in addition to the log text."""
self.messages.append(self.format(record))
def reset(self) -> None:
"""Empty the list of messages"""
self.messages = []