Skip to content

Commit ad4e687

Browse files
committed
Replace example for non-concurrent task with struclog
1 parent 81883ab commit ad4e687

File tree

1 file changed

+13
-5
lines changed
  • styleguide_example/blog_examples/celery_non_concurrent

1 file changed

+13
-5
lines changed

styleguide_example/blog_examples/celery_non_concurrent/tasks.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import logging
21
from functools import wraps
32

3+
import structlog
44
from celery import shared_task
55

66
from styleguide_example.tasks import celery_app
77

88
inspect = celery_app.control.inspect
99

10-
logger = logging.getLogger(__name__)
10+
logger = structlog.get_logger(__name__)
1111

1212

1313
def non_concurrent_task(_func=None, *args, **kwargs):
14+
log = logger.bind()
15+
1416
def wrapper(func):
1517
@wraps(func)
1618
def inner(_bound_self, *_func_args, **_func_kwargs):
@@ -27,7 +29,10 @@ def inner(_bound_self, *_func_args, **_func_kwargs):
2729
running_task_count += 1
2830

2931
if running_task_count > 1:
30-
logger.warning(f"[non_concurrent_task] Task {_bound_self.name} is already running")
32+
log.warning(
33+
"non_concurrent_task",
34+
message=f"[non_concurrent_task] Task {_bound_self.name} is already running",
35+
)
3136
return
3237

3338
return func(*_func_args, **_func_kwargs)
@@ -42,7 +47,10 @@ def inner(_bound_self, *_func_args, **_func_kwargs):
4247

4348
@non_concurrent_task
4449
def test_non_concurrent_task():
45-
logger.info("A non-concurrent task is running")
50+
log = logger.bind()
51+
52+
log.info("non_concurrent_task", message="A non-concurrent task is running")
4653
import time
54+
4755
time.sleep(10)
48-
logger.info("A non-concurrent task finished")
56+
log.info("non_concurrent_task", message="A non-concurrent task finished")

0 commit comments

Comments
 (0)