Skip to content

Commit 6dec95d

Browse files
committed
Add a mock log handler to collect formatted log messages
1 parent f89fc29 commit 6dec95d

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""Global fixtures and settings for the pytest test suite"""
2+
import sys
3+
import os
4+
5+
# Add test helper modules to search path with out making "tests" a Python package
6+
sys.path.append(os.path.join(os.path.dirname(__file__), "helpers"))

tests/helpers/handler.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import logging
2+
from typing import List
3+
4+
5+
class FormattedMessageCollectorHandler(logging.Handler):
6+
"""A logging handler that stores formatted log records in its "messages" attribute."""
7+
8+
def __init__(self, level=logging.NOTSET) -> None:
9+
"""Create a new log handler."""
10+
super().__init__(level=level)
11+
self.level = level
12+
self.messages: List[str] = []
13+
14+
def emit(self, record: logging.LogRecord) -> None:
15+
"""Keep the log records in a list in addition to the log text."""
16+
self.messages.append(self.format(record))
17+
18+
def reset(self) -> None:
19+
"""Empty the list of messages"""
20+
self.messages = []

0 commit comments

Comments
 (0)