Skip to content

Commit 2407b91

Browse files
committed
refactor(logging): improve logging setup and formatting consistency
- Refactored logging setup calls across multiple files to enhance readability by standardizing the formatting of parameters. - Cleaned up whitespace and improved indentation in logging-related code to maintain consistency and clarity. - Ensured that logging configuration updates are handled uniformly, contributing to better maintainability of the logging system.
1 parent 398d108 commit 2407b91

File tree

10 files changed

+69
-37
lines changed

10 files changed

+69
-37
lines changed

packages/qdrant-loader-core/src/qdrant_loader_core/logging.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,17 @@ class LoggingConfig:
193193
_initialized = False
194194
_installed_handlers: list[logging.Handler] = []
195195
_file_handler: logging.FileHandler | None = None
196-
_current_config: tuple[
197-
str, # level
198-
str, # format
199-
str | None, # file
200-
bool, # clean_output
201-
bool, # suppress_qdrant_warnings
202-
bool, # disable_console
203-
] | None = None
196+
_current_config: (
197+
tuple[
198+
str, # level
199+
str, # format
200+
str | None, # file
201+
bool, # clean_output
202+
bool, # suppress_qdrant_warnings
203+
bool, # disable_console
204+
]
205+
| None
206+
) = None
204207

205208
@classmethod
206209
def setup(
@@ -389,5 +392,14 @@ def reconfigure(cls, *, file: str | None = None) -> None:
389392

390393
# Update current config tuple if available
391394
if cls._current_config is not None:
392-
level, fmt, _, clean_output, suppress_qdrant_warnings, disable_console = cls._current_config
393-
cls._current_config = (level, fmt, file, clean_output, suppress_qdrant_warnings, disable_console)
395+
level, fmt, _, clean_output, suppress_qdrant_warnings, disable_console = (
396+
cls._current_config
397+
)
398+
cls._current_config = (
399+
level,
400+
fmt,
401+
file,
402+
clean_output,
403+
suppress_qdrant_warnings,
404+
disable_console,
405+
)

packages/qdrant-loader-core/tests/test_logging_core.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,26 @@ def test_redaction_filter_masks_and_marks(caplog):
66
caplog.set_level(logging.INFO)
77

88
logging_mod = import_module("qdrant_loader_core.logging")
9-
LoggingConfig = logging_mod.LoggingConfig
109
RedactionFilter = logging_mod.RedactionFilter
1110

1211
# Keep root handlers to restore after test to avoid side-effects
1312
root = logging.getLogger()
1413
prev_handlers = list(root.handlers)
15-
14+
1615
# Create a custom handler that captures messages after redaction
1716
captured_messages = []
18-
17+
1918
class TestHandler(logging.Handler):
2019
def emit(self, record):
2120
# Format the record to get the final message
2221
msg = self.format(record)
2322
captured_messages.append(msg)
24-
23+
2524
try:
2625
# Clear existing handlers and add our test handler with redaction filter
2726
for h in list(root.handlers):
2827
root.removeHandler(h)
29-
28+
3029
test_handler = TestHandler()
3130
test_handler.setLevel(logging.INFO)
3231
test_handler.addFilter(RedactionFilter())
@@ -41,13 +40,18 @@ def emit(self, record):
4140
logger.info("arg test %s", "sk-ABCDEFGH")
4241

4342
# Redaction marker should appear at least once
44-
assert any("***REDACTED***" in m for m in captured_messages), f"Messages: {captured_messages}"
43+
assert any(
44+
"***REDACTED***" in m for m in captured_messages
45+
), f"Messages: {captured_messages}"
4546
# Original secrets should not appear
46-
assert all("sk-ABCDEFGHIJKLMN" not in m for m in captured_messages), f"Messages: {captured_messages}"
47+
assert all(
48+
"sk-ABCDEFGHIJKLMN" not in m for m in captured_messages
49+
), f"Messages: {captured_messages}"
4750
# Mask should keep first/last 2 chars for long secrets.
4851
# Depending on exact pattern matched, the visible context may be key or value.
4952
assert any(
50-
("sk***REDACTED***MN" in m) or ("ap***REDACTED***MN" in m) for m in captured_messages
53+
("sk***REDACTED***MN" in m) or ("ap***REDACTED***MN" in m)
54+
for m in captured_messages
5155
), f"Messages: {captured_messages}"
5256
finally:
5357
# Restore handlers

packages/qdrant-loader-mcp-server/src/qdrant_loader_mcp_server/cli.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,16 @@ def _setup_logging(log_level: str, transport: str | None = None) -> None:
5555
# Only switch file target (none in stdio; may be env provided)
5656
LoggingConfig.reconfigure(file=os.getenv("MCP_LOG_FILE")) # type: ignore[attr-defined]
5757
else:
58-
LoggingConfig.setup(level=level, format=("json" if disable_console_logging else "console"))
58+
LoggingConfig.setup(
59+
level=level,
60+
format=("json" if disable_console_logging else "console"),
61+
)
5962
else:
6063
# Force replace handlers on older versions
6164
logging.getLogger().handlers = []
62-
LoggingConfig.setup(level=level, format=("json" if disable_console_logging else "console"))
65+
LoggingConfig.setup(
66+
level=level, format=("json" if disable_console_logging else "console")
67+
)
6368
except Exception as e:
6469
print(f"Failed to setup logging: {e}", file=sys.stderr)
6570

packages/qdrant-loader-mcp-server/tests/unit/test_cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def test_setup_logging_console_enabled(self, mock_logging_config):
107107
# Mock that LoggingConfig is not initialized and has no reconfigure method
108108
mock_logging_config._initialized = False
109109
mock_logging_config.reconfigure = None
110-
110+
111111
_setup_logging("DEBUG")
112112

113113
mock_logging_config.setup.assert_called_once_with(
@@ -121,7 +121,7 @@ def test_setup_logging_console_disabled(self, mock_logging_config):
121121
# Mock that LoggingConfig is not initialized and has no reconfigure method
122122
mock_logging_config._initialized = False
123123
mock_logging_config.reconfigure = None
124-
124+
125125
_setup_logging("INFO")
126126

127127
mock_logging_config.setup.assert_called_once_with(level="INFO", format="json")
@@ -133,7 +133,7 @@ def test_setup_logging_console_disabled_case_insensitive(self, mock_logging_conf
133133
# Mock that LoggingConfig is not initialized and has no reconfigure method
134134
mock_logging_config._initialized = False
135135
mock_logging_config.reconfigure = None
136-
136+
137137
_setup_logging("WARNING")
138138

139139
mock_logging_config.setup.assert_called_once_with(

packages/qdrant-loader-mcp-server/tests/unit/test_logging.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,10 @@ def test_logging_config_suppress_qdrant_warnings():
200200

201201
# Check if qdrant_client logger was accessed (either directly or via core)
202202
# The implementation may delegate to core logging which handles this
203-
logger_calls = [call[0][0] for call in mock_get_logger.call_args_list if call[0]]
204-
203+
logger_calls = [
204+
call[0][0] for call in mock_get_logger.call_args_list if call[0]
205+
]
206+
205207
# If core logging is available, it handles qdrant suppression internally
206208
# If not, the fallback should add the filter
207209
if "qdrant_client" in logger_calls:

packages/qdrant-loader/src/qdrant_loader/cli/commands/init_cmd.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,16 @@ async def run_init_command(
4444
if getattr(LoggingConfig, "_initialized", False): # type: ignore[attr-defined]
4545
LoggingConfig.reconfigure(file="qdrant-loader.log") # type: ignore[attr-defined]
4646
else:
47-
LoggingConfig.setup(level=log_level, format="console", file="qdrant-loader.log")
47+
LoggingConfig.setup(
48+
level=log_level, format="console", file="qdrant-loader.log"
49+
)
4850
else:
4951
import logging as _py_logging
5052

5153
_py_logging.getLogger().handlers = []
52-
LoggingConfig.setup(level=log_level, format="console", file="qdrant-loader.log")
54+
LoggingConfig.setup(
55+
level=log_level, format="console", file="qdrant-loader.log"
56+
)
5357
logger = LoggingConfig.get_logger(__name__)
5458

5559
# Check for updates (non-blocking semantics preserved by immediate return)

packages/qdrant-loader/src/qdrant_loader/config/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
# Load environment variables from .env file
4040
load_dotenv(override=False)
4141

42+
4243
def _get_logger():
4344
return LoggingConfig.get_logger(__name__)
4445

@@ -379,7 +380,9 @@ def from_yaml(
379380
# Step 1: Load environment variables first
380381
if env_path is not None:
381382
# Custom env file specified - load only this file
382-
_get_logger().debug("Loading custom environment file", path=str(env_path))
383+
_get_logger().debug(
384+
"Loading custom environment file", path=str(env_path)
385+
)
383386
if not env_path.exists():
384387
raise FileNotFoundError(f"Environment file not found: {env_path}")
385388
load_dotenv(env_path, override=True)

packages/qdrant-loader/src/qdrant_loader/config/workspace.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ def get_workspace_env_override(workspace_config: WorkspaceConfig) -> dict[str, s
154154
"STATE_DB_PATH": str(workspace_config.database_path),
155155
}
156156

157-
_get_logger().debug("Generated workspace environment overrides", overrides=overrides)
157+
_get_logger().debug(
158+
"Generated workspace environment overrides", overrides=overrides
159+
)
158160
return overrides
159161

160162

packages/qdrant-loader/tests/unit/config/test_workspace.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ def test_create_workspace_structure_new_directory(self):
268268
workspace_path = Path(temp_dir) / "new_workspace"
269269

270270
with patch("qdrant_loader.config.workspace._get_logger") as mock_get_logger:
271-
mock_logger = Mock()
272-
mock_get_logger.return_value = mock_logger
271+
mock_logger = Mock()
272+
mock_get_logger.return_value = mock_logger
273273
create_workspace_structure(workspace_path)
274274

275275
# Verify directories were created

packages/qdrant-loader/tests/unit/utils/test_logging_redaction.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ def test_logging_redacts_sensitive_fields(caplog):
1010

1111
# Create a custom handler to capture redacted messages
1212
captured_messages = []
13-
13+
1414
class TestHandler(logging.Handler):
1515
def emit(self, record):
1616
# Format the record to get the final message after redaction
1717
msg = self.format(record)
1818
captured_messages.append(msg)
19-
19+
2020
# Add our test handler to the root logger
2121
root_logger = logging.getLogger()
2222
test_handler = TestHandler()
2323
test_handler.setLevel(logging.DEBUG)
2424
root_logger.addHandler(test_handler)
25-
25+
2626
try:
2727
logger.info(
2828
"config_dump",
@@ -53,19 +53,19 @@ def test_stdlib_logs_are_redacted(caplog):
5353

5454
# Create a custom handler to capture redacted messages
5555
captured_messages = []
56-
56+
5757
class TestHandler(logging.Handler):
5858
def emit(self, record):
5959
# Format the record to get the final message after redaction
6060
msg = self.format(record)
6161
captured_messages.append(msg)
62-
62+
6363
# Add our test handler to the root logger
6464
root_logger = logging.getLogger()
6565
test_handler = TestHandler()
6666
test_handler.setLevel(logging.DEBUG)
6767
root_logger.addHandler(test_handler)
68-
68+
6969
try:
7070
logger.info(
7171
"Sending token=%s and api_key=%s",

0 commit comments

Comments
 (0)