summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozlog/tests/conftest.py
blob: eaf79897e4df09c81036c14fb919154d5d4d8982 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import pytest
from mozlog.formatters import ErrorSummaryFormatter, MachFormatter
from mozlog.handlers import StreamHandler
from mozlog.structuredlog import StructuredLogger
from six import StringIO


@pytest.fixture
def get_logger():
    # Ensure a new state instance is created for each test function.
    StructuredLogger._logger_states = {}
    formatters = {
        "mach": MachFormatter,
        "errorsummary": ErrorSummaryFormatter,
    }

    def inner(name, **fmt_args):
        buf = StringIO()
        fmt = formatters[name](**fmt_args)
        logger = StructuredLogger("test_logger")
        logger.add_handler(StreamHandler(buf, fmt))
        return logger

    return inner