|
| 1 | +""" |
| 2 | +Changes the log level of workflow task failures from WARN to ERROR. |
| 3 | +""" |
| 4 | + |
| 5 | +import asyncio, logging, sys |
| 6 | +from temporalio import workflow |
| 7 | +from temporalio.client import Client |
| 8 | +from temporalio.worker import Worker |
| 9 | + |
| 10 | +# --- Begin logging set‑up ---------------------------------------------------------- |
| 11 | +logging.basicConfig( |
| 12 | + stream=sys.stdout, |
| 13 | + level=logging.INFO, |
| 14 | + format="%(asctime)s %(levelname)-8s %(name)s %(message)s", |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +class CustomLogFilter(logging.Filter): |
| 19 | + def filter(self, record): |
| 20 | + msg = record.getMessage() |
| 21 | + if ( |
| 22 | + msg.startswith("Failed activation on workflow") |
| 23 | + and " with ID " in msg |
| 24 | + and " and run ID " in msg |
| 25 | + and record.levelno < logging.ERROR |
| 26 | + ): |
| 27 | + record.levelno = logging.ERROR |
| 28 | + record.levelname = logging.getLevelName(logging.ERROR) |
| 29 | + return True |
| 30 | + |
| 31 | + |
| 32 | +logging.getLogger("temporalio.worker._workflow_instance").addFilter(CustomLogFilter()) |
| 33 | +# --- End logging set‑up ---------------------------------------------------------- |
| 34 | + |
| 35 | + |
| 36 | +@workflow.defn |
| 37 | +class GreetingWorkflow: |
| 38 | + @workflow.run |
| 39 | + async def run(self): |
| 40 | + raise RuntimeError("This is a test error") |
| 41 | + |
| 42 | + |
| 43 | +async def main(): |
| 44 | + client = await Client.connect("localhost:7233") |
| 45 | + async with Worker( |
| 46 | + client, |
| 47 | + task_queue="hello-task-queue", |
| 48 | + workflows=[GreetingWorkflow], |
| 49 | + ): |
| 50 | + await client.execute_workflow( |
| 51 | + GreetingWorkflow.run, |
| 52 | + id="hello-workflow-id", |
| 53 | + task_queue="hello-task-queue", |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + asyncio.run(main()) |
0 commit comments