-
Notifications
You must be signed in to change notification settings - Fork 94
Change_log_level #187
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
Change_log_level #187
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
598c1af
added sample showing how to change log levels
GSmithApps 75af4c0
fixed isort
GSmithApps 48f104d
Responded to one comment
GSmithApps 9b93793
Responded to second comment
GSmithApps a1dc4d3
Merge branch 'temp-main' into changle_log_level
GSmithApps 8467966
first-try
GSmithApps 37f7978
added logging test
GSmithApps 65c60c2
test is passing
GSmithApps bfc22ca
responded to PR comments
GSmithApps 6c20201
responded to comment
GSmithApps 168d7e0
Merge branch 'main-from-temporal-aka-not-my-fork' into changle_log_level
GSmithApps b1bc492
responded to PR comments
GSmithApps 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| """ | ||
| Changes the log level of workflow task failures from WARN to ERROR. | ||
|
|
||
| Note that the __temporal_error_identifier attribute was added in | ||
| version 1.13.0 of the Python SDK. | ||
| """ | ||
|
|
||
| import asyncio | ||
| import logging | ||
| import sys | ||
|
|
||
| from temporalio import workflow | ||
| from temporalio.client import Client | ||
| from temporalio.worker import Worker | ||
|
|
||
| # --- Begin logging set‑up ---------------------------------------------------------- | ||
| logging.basicConfig( | ||
| stream=sys.stdout, | ||
| level=logging.INFO, | ||
| format="%(asctime)s %(levelname)-8s %(name)s %(message)s", | ||
| ) | ||
|
|
||
|
|
||
| class CustomLogFilter(logging.Filter): | ||
| def filter(self, record: logging.LogRecord) -> bool: | ||
| # Note that the __temporal_error_identifier attribute was added in | ||
| # version 1.13.0 of the Python SDK. | ||
| if ( | ||
| hasattr(record, "__temporal_error_identifier") | ||
| and getattr(record, "__temporal_error_identifier") == "WorkflowTaskFailure" | ||
| ): | ||
| record.levelno = logging.ERROR | ||
| record.levelname = logging.getLevelName(logging.ERROR) | ||
| return True | ||
|
|
||
|
|
||
| for h in logging.getLogger().handlers: | ||
| h.addFilter(CustomLogFilter()) | ||
| # --- End logging set‑up ---------------------------------------------------------- | ||
|
|
||
|
|
||
| LOG_MESSAGE = "This error is an experiment to check the log level" | ||
|
|
||
|
|
||
| @workflow.defn | ||
| class GreetingWorkflow: | ||
| @workflow.run | ||
| async def run(self): | ||
| raise RuntimeError(LOG_MESSAGE) | ||
|
|
||
|
|
||
| async def main(): | ||
| client = await Client.connect("localhost:7233") | ||
| async with Worker( | ||
| client, | ||
| task_queue="hello-change-log-level-task-queue", | ||
| workflows=[GreetingWorkflow], | ||
| ): | ||
| await client.execute_workflow( | ||
| GreetingWorkflow.run, | ||
| id="hello-change-log-level-workflow-id", | ||
| task_queue="hello-change-log-level-task-queue", | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
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,43 @@ | ||
| import asyncio | ||
| import io | ||
| import logging | ||
| import uuid | ||
|
|
||
| from temporalio.client import Client | ||
| from temporalio.worker import Worker | ||
|
|
||
| from hello.hello_change_log_level import LOG_MESSAGE, GreetingWorkflow | ||
|
|
||
|
|
||
| async def test_workflow_with_log_capture(client: Client): | ||
|
|
||
| log_stream = io.StringIO() | ||
| handler = logging.StreamHandler(log_stream) | ||
| handler.setLevel(logging.ERROR) | ||
|
|
||
| logger = logging.getLogger() | ||
| logger.addHandler(handler) | ||
| logger.setLevel(logging.DEBUG) | ||
|
|
||
| task_queue = f"tq-{uuid.uuid4()}" | ||
|
|
||
| async with Worker( | ||
| client, | ||
| task_queue=task_queue, | ||
| workflows=[GreetingWorkflow], | ||
| ): | ||
| handle = await client.start_workflow( | ||
| GreetingWorkflow.run, | ||
| id=f"wf-{uuid.uuid4()}", | ||
| task_queue=task_queue, | ||
| ) | ||
| await asyncio.sleep( | ||
| 0.2 | ||
| ) # small wait to ensure the workflow has started, failed, and logged | ||
| await handle.terminate() | ||
|
|
||
| logger.removeHandler(handler) | ||
| handler.flush() | ||
|
|
||
| logs = log_stream.getvalue() | ||
| assert LOG_MESSAGE in logs |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be able to add a test for this to confirm continued expected behavior (though I know we haven't always added tests for all samples)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for keeping me honest!
Speaking of that, I vibe coded a test that that checks what we want: it passes with the code as-is, but it fails if I remove the sample's configuration to elevate the logging.