fix(config): restore log.file configuration for v4.1 multi-provider format#564
fix(config): restore log.file configuration for v4.1 multi-provider format#564
Conversation
…ormat
The _setup_logging function was incorrectly using an empty dict for log settings
when there were multiple providers in v4.1 format. This caused log.file and other
log configuration to be ignored.
Changed the condition from:
json_config = all_json_configs[0] if len(all_json_configs) == 1 else {}
To:
json_config = all_json_configs[0] if all_json_configs else {}
Since all provider configs inherit the same global log settings in v4.1 format,
we can safely use the first config for logging setup regardless of the number
of providers.
Co-authored-by: NewFuture <[email protected]>
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug in DDNS v4.1 where the log.file configuration was ignored when using the new multi-provider format. The issue occurred because logging setup logic incorrectly used an empty configuration for log settings when multiple providers were configured.
- Fixed conditional logic in
_setup_loggingto always use the first config for log settings - Added comprehensive regression test to verify log file creation with multiple providers
- Ensures backward compatibility while fixing the reported logging issue
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| ddns/config/init.py | Fixed the conditional logic in _setup_logging to use first config for log settings instead of empty dict |
| tests/test_config_schema_v4_1.py | Added regression test test_v41_multi_provider_log_config to verify log file functionality with multiple providers |
Move sys and logging imports from inside test method to the top of the file, following standard Python import conventions and matching the pattern used in other test files in the repository. Co-authored-by: NewFuture <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
|
@copilot fix lint |
- Use logging.INFO constant instead of hardcoded 20 for Python 2.7 compatibility - Remove trailing whitespace and fix blank lines with whitespace (W291, W293) Co-authored-by: NewFuture <[email protected]>
... Fixed Python 2.7 compatibility by using |
|
@copilot fix py2.7 tests (success in py3.x) |
Use assertIn to check for multiple acceptable log_level values since getLevelName behaves differently in Python 2.7 vs 3.x. The test now accepts integer 20, logging.INFO constant, or string "INFO" to ensure compatibility across Python versions. Co-authored-by: NewFuture <[email protected]>
... Fixed Python 2.7 compatibility by using |
Co-authored-by: Copilot <[email protected]>
Fix log.file configuration in v4.1 multi-provider format
Problem Summary
In DDNS v4.1, the
log.fileconfiguration was ignored when using the newprovidersarray format with multiple providers. Console logging worked normally and domain updates proceeded, but logs were not written to the specified file.User Report: "之前4.02版本这么写,log file记录都是正常的。更新到了4.1 log is正常显示,域名更新也是正常的,但是file里面不记录了"
Root Cause Analysis
The
_setup_loggingfunction inddns/config/__init__.pyhad a condition that only used log settings from the first config whenlen(all_json_configs) == 1:When using the v4.1 providers format with multiple providers,
len(all_json_configs) > 1, causing an empty dict{}to be used, which lost all log configuration includinglog.file.The Fix
Changed one line to always use the first config for log settings:
Why this is safe: In v4.1 format, all provider configs inherit the same global log settings from the parent configuration. The log configuration is identical across all providers, so using the first config is correct.
Changes Made
ddns/config/__init__.py: Changed condition in_setup_loggingfunction (1 line)test_v41_multi_provider_log_configregression test intests/test_config_schema_v4_1.pyTest Results
✅ All tests pass (Python 2.7 and 3.x compatible)
Impact
Example Fixed Configuration
{ "$schema": "https://ddns.newfuture.cc/schema/v4.1.json", "log": { "level": "INFO", "file": "/var/log/ddns.log", "format": "%(asctime)s %(levelname)s [%(filename)s:%(lineno)d]: %(message)s", "datefmt": "%Y-%m-%d %H:%M:%S" }, "providers": [ {"provider": "cloudflare", "id": "[email protected]", "token": "token1", "ipv4": ["domain1.com"]}, {"provider": "dnspod", "id": "[email protected]", "token": "token2", "ipv4": ["domain2.com"]} ] }Now logs are correctly written to
/var/log/ddns.logeven with multiple providers! 🎉Original prompt
Fixes #563
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.