| 1 |
|
| 2 |
|
| 3 |
"""Helper functions for the regex test tool.""" |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import functools |
| 8 |
import logging |
| 9 |
import sys |
| 10 |
import typing |
| 11 |
|
| 12 |
|
| 13 |
if typing.TYPE_CHECKING: |
| 14 |
from typing import Final |
| 15 |
|
| 16 |
|
| 17 |
@functools.lru_cache |
| 18 |
def build_logger(*, quiet: bool = False, verbose: bool = False) -> logging.Logger: |
| 19 |
"""Build a logger that outputs to the standard output and error streams. |
| 20 |
|
| 21 |
Messages of level `WARNING` and higher go to the standard error stream. |
| 22 |
If `quiet` is false, messages of level `INFO` go to the standard error stream. |
| 23 |
If `verbose` is true, messages of level `DEBUG` also go to the standard error stream. |
| 24 |
""" |
| 25 |
logger: Final = logging.getLogger("temptest") |
| 26 |
logger.setLevel(logging.DEBUG if verbose else logging.WARNING if quiet else logging.INFO) |
| 27 |
logger.propagate = False |
| 28 |
|
| 29 |
diag_handler: Final = logging.StreamHandler(sys.stderr) |
| 30 |
diag_handler.setLevel(logging.DEBUG if verbose else logging.WARNING) |
| 31 |
if not quiet: |
| 32 |
diag_handler.addFilter(lambda rec: rec.levelno != logging.INFO) |
| 33 |
logger.addHandler(diag_handler) |
| 34 |
|
| 35 |
if not quiet: |
| 36 |
info_handler: Final = logging.StreamHandler(sys.stderr) |
| 37 |
info_handler.setLevel(logging.INFO) |
| 38 |
info_handler.addFilter(lambda rec: rec.levelno == logging.INFO) |
| 39 |
logger.addHandler(info_handler) |
| 40 |
|
| 41 |
return logger |