Internal logging utility: _DeferredString#205
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a reusable _DeferredString utility class to optimize logging performance by deferring expensive string operations until they're actually needed. The implementation replaces a custom deferred logging class in the MSAL authentication code with this new generalized utility.
Key Changes:
- Added a new
_DeferredStringutility class that defers function execution until string conversion - Refactored MSAL authentication logging to use the new utility instead of a custom implementation
- Improved code maintainability by centralizing deferred string evaluation logic
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
_deferred_string.py |
Introduces the _DeferredString class for deferred function evaluation |
_utils/__init__.py |
Exports the _DeferredString class from the utils module |
msal_auth.py |
Replaces custom _DeferredLogOfBlueprintId class with _DeferredString utility |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
libraries/microsoft-agents-activity/microsoft_agents/activity/_utils/_deferred_string.py
Show resolved
Hide resolved
...aries/microsoft-agents-authentication-msal/microsoft_agents/authentication/msal/msal_auth.py
Show resolved
Hide resolved
cleemullins
left a comment
There was a problem hiding this comment.
This is nice.
Are there other places we're doing logging that this should be used?
I image there are a number of "Activity Log" type calls, which need to be replaced. I think Claude 4.5 or Copilot should be able to find them all for you quick quickly.
Fortunately, those are already handled by replacing format strings substitution with "%s" for lazy evaluation. We don't often go out of our way to perform lazy computation in this codebase. I asked Copilot, and all I found were uses of format strings. There's a lot of those, and I already started the conversion in the UserTokenClient and ConnectorClient classes in a separate PR. Thus, I will proceed and merge this. |
This pull request introduces a new utility for deferred string evaluation and refactors logging in the MSAL authentication code to improve performance and maintainability. The main changes are the addition of the
_DeferredStringclass and its use for deferred logging, replacing a custom implementation.Deferred string evaluation utility:
_DeferredStringclass inmicrosoft_agents/activity/_utils/_deferred_string.pyto allow deferred evaluation of expensive string operations, improving logging efficiency._DeferredStringthrough the package’s__init__.pyfor easier import.Refactoring logging in MSAL authentication:
_DeferredLogOfBlueprintIdclass frommsal_auth.pyand replaced its usage with the new_DeferredStringutility for deferred logging of decoded JWT blueprint IDs.get_agentic_instance_tokento use_DeferredString, ensuring JWT decoding only occurs if the log level is set to DEBUG.msal_auth.pyfor use in logging.