|
| 1 | +""" |
| 2 | +Changes the log level of workflow task failures from WARN to ERROR. |
| 3 | +
|
| 4 | +Note that the __temporal_error_identifier attribute was added in |
| 5 | +version 1.13.0 of the Python SDK. |
| 6 | +""" |
| 7 | + |
| 8 | +import asyncio |
| 9 | +import logging |
| 10 | +import sys |
| 11 | + |
| 12 | +from temporalio import workflow |
| 13 | +from temporalio.client import Client |
| 14 | +from temporalio.worker import Worker |
| 15 | + |
| 16 | +# --- Begin logging set‑up ---------------------------------------------------------- |
| 17 | +logging.basicConfig( |
| 18 | + stream=sys.stdout, |
| 19 | + level=logging.INFO, |
| 20 | + format="%(asctime)s %(levelname)-8s %(name)s %(message)s", |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +class CustomLogFilter(logging.Filter): |
| 25 | + def filter(self, record: logging.LogRecord) -> bool: |
| 26 | + # Note that the __temporal_error_identifier attribute was added in |
| 27 | + # version 1.13.0 of the Python SDK. |
| 28 | + if ( |
| 29 | + hasattr(record, "__temporal_error_identifier") |
| 30 | + and getattr(record, "__temporal_error_identifier") == "WorkflowTaskFailure" |
| 31 | + ): |
| 32 | + record.levelno = logging.ERROR |
| 33 | + record.levelname = logging.getLevelName(logging.ERROR) |
| 34 | + return True |
| 35 | + |
| 36 | + |
| 37 | +for h in logging.getLogger().handlers: |
| 38 | + h.addFilter(CustomLogFilter()) |
| 39 | +# --- End logging set‑up ---------------------------------------------------------- |
| 40 | + |
| 41 | + |
| 42 | +LOG_MESSAGE = "This error is an experiment to check the log level" |
| 43 | + |
| 44 | + |
| 45 | +@workflow.defn |
| 46 | +class GreetingWorkflow: |
| 47 | + @workflow.run |
| 48 | + async def run(self): |
| 49 | + raise RuntimeError(LOG_MESSAGE) |
| 50 | + |
| 51 | + |
| 52 | +async def main(): |
| 53 | + client = await Client.connect("localhost:7233") |
| 54 | + async with Worker( |
| 55 | + client, |
| 56 | + task_queue="hello-change-log-level-task-queue", |
| 57 | + workflows=[GreetingWorkflow], |
| 58 | + ): |
| 59 | + await client.execute_workflow( |
| 60 | + GreetingWorkflow.run, |
| 61 | + id="hello-change-log-level-workflow-id", |
| 62 | + task_queue="hello-change-log-level-task-queue", |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + asyncio.run(main()) |
0 commit comments