Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions tests/unit/test_config_root_level.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import configparser
import importlib.util
import logging
from pathlib import Path

import pytest

pytestmark = pytest.mark.core


@pytest.fixture
def isolated_logging_config(monkeypatch, tmp_path):
"""Load the stdlib-only utility without initializing the SDK's logging."""
source = Path(__file__).resolve().parents[2] / "tinytroupe/utils/config.py"
spec = importlib.util.spec_from_file_location("isolated_logging_config", source)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
root = logging.getLogger()
project = logging.getLogger("tinytroupe")
root_handlers, root_level = root.handlers[:], root.level
project_handlers, project_level = project.handlers[:], project.level
project_propagate = project.propagate
monkeypatch.chdir(tmp_path)
try:
yield module
finally:
for handler in root.handlers[:]:
if handler not in root_handlers:
root.removeHandler(handler)
handler.close()
root.setLevel(root_level)
project.handlers[:] = project_handlers
project.setLevel(project_level)
project.propagate = project_propagate


@pytest.mark.parametrize(
"level, console, expected",
[
("DEBUG", "WARNING", logging.DEBUG),
("ERROR", "OFF", logging.ERROR),
("INFO", "INFO", logging.INFO),
],
)
def test_start_logger_applies_root_level(
isolated_logging_config, level, console, expected
):
settings = configparser.ConfigParser()
settings["Logging"] = {
"LOGLEVEL": level,
"LOGLEVEL_CONSOLE": console,
"LOGLEVEL_FILE": "OFF",
}
isolated_logging_config.start_logger(settings)
assert logging.getLogger().level == expected
assert logging.getLogger("tinytroupe").level == expected


def test_start_logger_replaces_previous_root_level(isolated_logging_config):
isolated_logging_config.set_loglevel("CRITICAL")
settings = configparser.ConfigParser()
settings["Logging"] = {
"LOGLEVEL": "DEBUG",
"LOGLEVEL_CONSOLE": "ERROR",
"LOGLEVEL_FILE": "OFF",
}
isolated_logging_config.start_logger(settings)
assert logging.getLogger().level == logging.DEBUG
assert logging.getLogger("tinytroupe").level == logging.DEBUG
2 changes: 1 addition & 1 deletion tinytroupe/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def emit(self, record):


def start_logger(config: configparser.ConfigParser):
global _log_file_path, _console_handler, _file_handler, _console_level, _file_level, _include_thread_info
global _log_file_path, _console_handler, _file_handler, _root_level, _console_level, _file_level, _include_thread_info

# Collect changes under lock, but avoid calling logging APIs while holding it.
with _logging_lock:
Expand Down