{Log} Adapt az_command_data_logger to Knack 0.8.0#17324
Merged
Conversation
jiasli
commented
Mar 16, 2021
Comment on lines
+205
to
+206
| with mock.patch('azure.cli.core.util.handle_exception', _handle_exception_with_log): | ||
| result = execute(cli_ctx, command, expect_failure=expect_failure).assert_with_checks(checks) |
Member
Author
There was a problem hiding this comment.
The old code registers Logger as a Handler. It works simply because both Logger and Handler expose handle method by coincidence. It is never guaranteed to work as such and may break any time the internal logic of logging module changes.
jiasli
commented
Mar 16, 2021
| # Print the traceback and exception message | ||
| logger.debug(traceback.format_exc()) | ||
|
|
||
| with CommandLoggerContext(logger): |
Member
Author
There was a problem hiding this comment.
CommandLoggerContext will be called again at:
azure-cli/src/azure-cli-core/azure/cli/core/azclierror.py
Lines 62 to 65 in 4560e96
Calling it here is redundant.
Besides, no log is written here, so these lines shouldn't be surrounded by CommandLoggerContext.
jiasli
commented
Mar 16, 2021
Comment on lines
+73
to
+82
| # print recommendations to action | ||
| if self.recommendations: | ||
| for recommendation in self.recommendations: | ||
| print(recommendation, file=sys.stderr) | ||
|
|
||
| if self.aladdin_recommendations: | ||
| print('\nTRY THIS:', file=sys.stderr) | ||
| for recommendation, description in self.aladdin_recommendations: | ||
| print_styled_text(recommendation, file=sys.stderr) | ||
| print_styled_text(description, file=sys.stderr) |
Member
Author
There was a problem hiding this comment.
No log is written here. The scope of CommandLoggerContext should be as small as possible.
Collaborator
|
Log |
fengzhou-msft
approved these changes
Mar 26, 2021
houk-ms
approved these changes
Mar 26, 2021
evelyn-ys
approved these changes
Mar 26, 2021
az_command_data_logger to Knack 0.8.0
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adapt to microsoft/knack#228: [Log]
CLILogging.configurereturns as early as possibleSymptom
az feedbackno longer works with knackdevbranch:Cause
In
pytestenvironment,pytestwill configure therootlogger to capture all logs.The old knack always forcibly set
rootlogger level toDEBUG.The new knack can detect that and it leaves
pytestrootlogger as is ([Log] CLILogging.configure returns as early as possible microsoft/knack#228).In such case, we have to manually set
az_command_data_logger's level toDEBUG, otherwise it will honorpytestrootlogger's level and beWARNINGby default (when--log-levelis not set). This makesaz_command_data_logger's log be ignored. For more info on how Python logging works, seelogging.Logger.getEffectiveLevel.Additional Context
az feedbackrelies onaz_command_data_logger(which are written to~/.azure/commands/).az_command_data_loggeris written byazure.cli.core.util.handle_exception.During testing,
azure.cli.core.util.handle_exceptionis mocked byazure.cli.testsdk.patches.patch_main_exception_handlerso that the originalExceptioncan be thrown and asserted.However,
azure.cli.testsdk.patches.patch_main_exception_handlerdoesn't write toaz_command_data_logger.In the oldest code ([wip] az feedback #8829), this is worked around by calling the leaked file handler after
executefinishes.As [Core] fix logging file fd leaking #13102 fixed the leaked file handler, the old code no longer worked. To work around it again, [Core] fix logging file fd leaking #13102 manually delayed the cleanup for file handler.
This PR refactors [Core] fix logging file fd leaking #13102 with a more elegant
mock.patchonazure.cli.core.util.handle_exceptionagain, so that no manual delay of file handler cleanup is necessary.