Repository: celery/celery

Issue:
Include args in Received (like Rejected)
- [X] I have checked the [issues list](https://github.com/celery/celery/issues?q=is%3Aissue+label%3A%22Issue+Type%3A+Enhancement%22+-label%3A%22Category%3A+Documentation%22)
  for similar or identical enhancement to an existing feature.
- [X] I have checked the [pull requests list](https://github.com/celery/celery/pulls?q=is%3Apr+label%3A%22Issue+Type%3A+Enhancement%22+-label%3A%22Category%3A+Documentation%22)
  for existing proposed enhancements.
- [X] I have checked the [commit log](https://github.com/celery/celery/commits/master)
  to find out if the if the same enhancement was already implemented in the
  master branch.
- [X] I have included all related issues and possible duplicate issues in this issue
      (If there are none, check this box anyway).

## Related Issues and Possible Duplicates
None that I'm aware of

# Brief Summary
Logging `args` is really helpful, but it's currently only done with `Rejected` logs and it would be nice to also include in `Received` logs

See https://github.com/celery/celery/blob/f02d7c60051ce5202349fe7c795ebf5000d9526d/celery/app/trace.py#L259

# Design

## Architectural Considerations
If a task takes a lot of args or large args, then it could make logs large

## Proposed Behavior
Include `args` in `Received` logs

## Proposed UI/UX
On by default with a way to turn it off

## Diagrams
N/A

## Alternatives
Off by default with a way to turn it on

Include args in Received (like Rejected)
- [X] I have checked the [issues list](https://github.com/celery/celery/issues?q=is%3Aissue+label%3A%22Issue+Type%3A+Enhancement%22+-label%3A%22Category%3A+Documentation%22)
  for similar or identical enhancement to an existing feature.
- [X] I have checked the [pull requests list](https://github.com/celery/celery/pulls?q=is%3Apr+label%3A%22Issue+Type%3A+Enhancement%22+-label%3A%22Category%3A+Documentation%22)
  for existing proposed enhancements.
- [X] I have checked the [commit log](https://github.com/celery/celery/commits/master)
  to find out if the if the same enhancement was already implemented in the
  master branch.
- [X] I have included all related issues and possible duplicate issues in this issue
      (If there are none, check this box anyway).

## Related Issues and Possible Duplicates
None that I'm aware of

# Brief Summary
Logging `args` is really helpful, but it's currently only done with `Rejected` logs and it would be nice to also include in `Received` logs

See https://github.com/celery/celery/blob/f02d7c60051ce5202349fe7c795ebf5000d9526d/celery/app/trace.py#L259

# Design

## Architectural Considerations
If a task takes a lot of args or large args, then it could make logs large

## Proposed Behavior
Include `args` in `Received` logs

## Proposed UI/UX
On by default with a way to turn it off

## Diagrams
N/A

## Alternatives
Off by default with a way to turn it on



Discussion:
Hey @daveisfera :wave:,
Thank you for opening an issue. We will get back to you as soon as we can.
Also, check out our [Open Collective](https://opencollective.com/celery) and consider backing us - every little helps!

We also offer priority support for our sponsors.
If you require immediate assistance please consider sponsoring us.

Hey @daveisfera :wave:,
Thank you for opening an issue. We will get back to you as soon as we can.
Also, check out our [Open Collective](https://opencollective.com/celery) and consider backing us - every little helps!

We also offer priority support for our sponsors.
If you require immediate assistance please consider sponsoring us.


Resolving patch:
diff --git a/celery/app/trace.py b/celery/app/trace.py
--- a/celery/app/trace.py
+++ b/celery/app/trace.py
@@ -527,6 +527,8 @@ def trace_task(uuid, args, kwargs, request=None):
                                 'name': get_task_name(task_request, name),
                                 'return_value': Rstr,
                                 'runtime': T,
+                                'args': safe_repr(args),
+                                'kwargs': safe_repr(kwargs),
                             })
 
                 # -* POST *-
diff --git a/celery/worker/strategy.py b/celery/worker/strategy.py
--- a/celery/worker/strategy.py
+++ b/celery/worker/strategy.py
@@ -2,6 +2,7 @@
 import logging
 
 from kombu.asynchronous.timer import to_timestamp
+from kombu.utils.encoding import safe_repr
 
 from celery import signals
 from celery.app import trace as _app_trace
@@ -151,7 +152,12 @@ def task_message_handler(message, body, ack, reject, callbacks,
         if _does_info:
             # Similar to `app.trace.info()`, we pass the formatting args as the
             # `extra` kwarg for custom log handlers
-            context = {'id': req.id, 'name': req.name}
+            context = {
+                'id': req.id,
+                'name': req.name,
+                'args': safe_repr(req.args),
+                'kwargs': safe_repr(req.kwargs),
+            }
             info(_app_trace.LOG_RECEIVED, context, extra={'data': context})
         if (req.expires or req.id in revoked_tasks) and req.revoked():
             return


Test patch:
diff --git a/t/unit/worker/test_strategy.py b/t/unit/worker/test_strategy.py
--- a/t/unit/worker/test_strategy.py
+++ b/t/unit/worker/test_strategy.py
@@ -191,7 +191,7 @@ def test_log_task_received_custom(self, caplog):
             C()
         for record in caplog.records:
             if record.msg == custom_fmt:
-                assert set(record.args) == {"id", "name"}
+                assert set(record.args) == {"id", "name", "kwargs", "args"}
                 break
         else:
             raise ValueError("Expected message not in captured log records")