My plan is to log all celery task requests with the arguments they receive s I'm using the following
@task_prerun.connect()
def log_celery_args(task, **kwargs):
logger.info(f"{task.name} {kwargs['kwargs']}", extra=kwargs['kwargs'])
The problem is this solution doesn't work when the functions don't use named parameters.
I can get the values from the kwargs['args']
but I'd like to still have in the extras the format
{arg_name: value}
So my idea was if there's a way to get the signature of a task or any other way to actually get the parameters needed ?
I already tried the following :
task
and task.request
to see if I can find it theresignature
function but this one is actually to change itcelery.app.trace
to see if I can get it to log with the parametersFrom Python 3.8 onwards you can use f"{kwargs=}"
to print all keyword arguments.
If positional arguments need to be allowed and logged then the function definition must be adapted as follows:
@task_prerun.connect()
def log_celery_task_call(task, *args, **kwargs):
logger.info(f"{task.name} {args=} {kwargs=}")
*args
in the function definition will catch all positional arguments and **kwargs
all keyword arguments. If there are no positional arguments the args
variable will be an empty list and if no keyword arguments are supplied kwargs
will be an empty dictionary.
See also the Python documentation for function definitions and calls.