Skip to content

Commit 5cfa4c5

Browse files
committed
added sample showing how to change log levels
1 parent 631544a commit 5cfa4c5

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Some examples require extra dependencies. See each sample's directory for specif
4141
* [hello_async_activity_completion](hello/hello_async_activity_completion.py) - Complete an activity outside of the
4242
function that was called.
4343
* [hello_cancellation](hello/hello_cancellation.py) - Manually react to cancellation inside workflows and activities.
44+
* [hello_change_log_level](hello/hello_change_log_level.py) - Change the level of workflow task failure from WARN to ERROR.
4445
* [hello_child_workflow](hello/hello_child_workflow.py) - Execute a child workflow from a workflow.
4546
* [hello_continue_as_new](hello/hello_continue_as_new.py) - Use continue as new to restart a workflow.
4647
* [hello_cron](hello/hello_cron.py) - Execute a workflow once a minute.

hello/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Replace `hello_activity.py` in the command with any other example filename to ru
2828
* [hello_async_activity_completion](hello_async_activity_completion.py) - Complete an activity outside of the function
2929
that was called.
3030
* [hello_cancellation](hello_cancellation.py) - Manually react to cancellation inside workflows and activities.
31+
* [hello_change_log_level](hello_change_log_level.py) - Change the level of workflow task failure from WARN to ERROR.
3132
* [hello_child_workflow](hello_child_workflow.py) - Execute a child workflow from a workflow.
3233
* [hello_continue_as_new](hello_continue_as_new.py) - Use continue as new to restart a workflow.
3334
* [hello_cron](hello_cron.py) - Execute a workflow once a minute.

hello/hello_change_log_level.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

Comments
 (0)