Skip to content

Commit 1be8c8a

Browse files
committed
Refactor logging configuration in BloodHound CLI to ensure it is set up only once using caching. This change removes the global logger initialization flag and introduces a new cached function for configuring logging, improving efficiency and clarity.
1 parent bbd0762 commit 1be8c8a

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

src/bloodhound_cli/core/logging_utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55
import logging
66
import sys
7+
from functools import lru_cache
78
from typing import Any, Dict
89

910
import structlog
1011

11-
_logger_initialized = False
12-
1312

1413
def configure_logging(debug: bool = False, json_output: bool = False) -> None:
1514
"""Configure structlog + standard logging once.
@@ -18,10 +17,12 @@ def configure_logging(debug: bool = False, json_output: bool = False) -> None:
1817
debug: Whether to emit debug-level events.
1918
json_output: Emit logs as JSON instead of human-readable console output.
2019
"""
21-
global _logger_initialized # pylint: disable=global-statement
22-
if _logger_initialized:
23-
return
20+
_configure_logging_once(debug, json_output)
21+
2422

23+
@lru_cache(maxsize=1)
24+
def _configure_logging_once(debug: bool, json_output: bool) -> None:
25+
"""Configure structlog exactly once, caching by arguments."""
2526
level = logging.DEBUG if debug else logging.INFO
2627
logging.basicConfig(level=level, stream=sys.stderr, format="%(message)s")
2728

@@ -43,7 +44,6 @@ def configure_logging(debug: bool = False, json_output: bool = False) -> None:
4344
wrapper_class=structlog.make_filtering_bound_logger(level),
4445
cache_logger_on_first_use=True,
4546
)
46-
_logger_initialized = True
4747

4848

4949
def get_logger(name: str | None = None, **initial_context: Any) -> structlog.BoundLogger:

0 commit comments

Comments
 (0)