-
Notifications
You must be signed in to change notification settings - Fork 16
Refactor xCDAT logging for consistency and safety #811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2c513a4
Fix root logger config to prevent duplicate outputs
tomvothecoder d28d0c9
Update _setup_root_logger() to not add stream handler
tomvothecoder 46939e3
Replace _setup_root_logger with _setup_xcdat_logger
tomvothecoder c0cec21
Address copilot comments
tomvothecoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import logging | ||
|
|
||
| import pytest | ||
|
|
||
| from xcdat._logger import _setup_custom_logger, _setup_xcdat_logger | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def reset_logging(): | ||
| # Reset logging before each test. | ||
| logging.shutdown() | ||
|
|
||
| for handler in logging.root.handlers[:]: | ||
| logging.root.removeHandler(handler) | ||
| yield | ||
|
|
||
| # Cleanup after test. | ||
| logging.shutdown() | ||
|
|
||
|
|
||
| class TestSetupXCDATLogger: | ||
| def test_logger_creation(self): | ||
| logger = _setup_xcdat_logger() | ||
|
|
||
| assert isinstance(logger, logging.Logger) | ||
| assert logger.name == "xcdat" | ||
|
|
||
| def test_logger_level(self): | ||
| logger = _setup_xcdat_logger(level=logging.DEBUG) | ||
|
|
||
| assert logger.level == logging.DEBUG | ||
|
|
||
| def test_logger_force(self): | ||
| logger = _setup_xcdat_logger() | ||
| handler_ids_before = [id(h) for h in logger.handlers] | ||
|
|
||
| # Force reconfiguration. | ||
| logger = _setup_xcdat_logger(force=True) | ||
|
|
||
| handler_ids_after = [id(h) for h in logger.handlers] | ||
|
|
||
| # Clean reset with one handler. | ||
| assert len(logger.handlers) == 1 | ||
| # The handler should be a new instance, not the same object. | ||
| assert handler_ids_before != handler_ids_after | ||
|
|
||
| def test_logger_format(self): | ||
| logger = _setup_xcdat_logger() | ||
| handler = logger.handlers[0] | ||
|
|
||
| assert isinstance(handler.formatter, logging.Formatter) | ||
| assert handler.formatter._fmt == ( | ||
| "%(asctime)s [%(levelname)s]: %(filename)s(%(funcName)s:%(lineno)s) >> %(message)s" | ||
| ) | ||
tomvothecoder marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def test_logger_propagation(self): | ||
| logger = _setup_xcdat_logger() | ||
|
|
||
| assert logger.propagate is False | ||
|
|
||
| def test_logger_no_duplicate_handlers(self): | ||
| logger1 = _setup_xcdat_logger() | ||
| num_handlers_before = len(logger1.handlers) | ||
|
|
||
| logger2 = _setup_xcdat_logger() | ||
| num_handlers_after = len(logger2.handlers) | ||
|
|
||
| assert num_handlers_after == num_handlers_before | ||
|
|
||
|
|
||
| class TestSetupCustomLogger: | ||
| def test_custom_logger_creation(self): | ||
| logger = _setup_custom_logger("test_logger") | ||
|
|
||
| assert isinstance(logger, logging.Logger) | ||
| assert logger.name == "test_logger" | ||
|
|
||
| def test_custom_logger_propagation(self): | ||
| logger = _setup_custom_logger("test_logger", propagate=False) | ||
| assert logger.propagate is False | ||
|
|
||
| logger = _setup_custom_logger("test_logger", propagate=True) | ||
| assert logger.propagate is True | ||
|
|
||
| def test_custom_logger_inherits_from_xcdat(self): | ||
| _setup_xcdat_logger() | ||
| logger = _setup_custom_logger("xcdat.submodule") | ||
|
|
||
| assert logger.parent is not None and logger.parent.name == "xcdat" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.