1- import logging
21from functools import wraps
32
3+ import structlog
44from celery import shared_task
55
66from styleguide_example .tasks import celery_app
77
88inspect = celery_app .control .inspect
99
10- logger = logging . getLogger (__name__ )
10+ logger = structlog . get_logger (__name__ )
1111
1212
1313def 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
4449def 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