| """Tests for logging_utils.py.""" |
|
|
| import logging |
| import re |
|
|
| from discord_support_issues.logging_utils import ( |
| LOGGER_NAME, |
| configure_project_logging, |
| get_logger, |
| ) |
|
|
|
|
| def test_console_suppressed_logs_still_write_to_file(tmp_path, capsys): |
| log_path = tmp_path / "project.log" |
|
|
| try: |
| configure_project_logging(level=logging.INFO, log_file=str(log_path)) |
| logger = get_logger("tests.logging") |
|
|
| logger.info("visible log line") |
| logger.info( |
| "hidden log line", |
| extra={"show_in_console": False}, |
| ) |
|
|
| for handler in logging.getLogger(LOGGER_NAME).handlers: |
| handler.flush() |
|
|
| captured = capsys.readouterr() |
| assert "visible log line" in captured.err |
| assert "hidden log line" not in captured.err |
| assert captured.out == "" |
|
|
| log_text = log_path.read_text(encoding="utf-8") |
| assert "visible log line" in log_text |
| assert "hidden log line" in log_text |
| finally: |
| configure_project_logging(level=logging.INFO, log_file=None) |
|
|
|
|
| def test_logs_directory_uses_timestamped_filename(tmp_path): |
| log_dir = tmp_path / "logs" |
| requested_path = log_dir / "discord-support-issues.log" |
|
|
| try: |
| configure_project_logging(level=logging.INFO, log_file=str(requested_path)) |
| logger = get_logger("tests.logging") |
| logger.info("timestamped log line") |
|
|
| for handler in logging.getLogger(LOGGER_NAME).handlers: |
| if handler.get_name() == "file": |
| handler.flush() |
|
|
| log_files = list(log_dir.glob("*.log")) |
| assert len(log_files) == 1 |
| assert re.fullmatch( |
| rf"{re.escape(requested_path.stem)}-\d{{8}}-\d{{6}}\.log", |
| log_files[0].name, |
| ) |
| assert "timestamped log line" in log_files[0].read_text(encoding="utf-8") |
| finally: |
| configure_project_logging(level=logging.INFO, log_file=None) |
|
|