-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bind a "command_id" when using django commands #310
Comments
We've done exactly this on some of our projects and it works like a charm! |
Thanks @bcdickinson, this is reassuring. I was currently working on implementing it. The only thing I am not sure about it when there is an unhandled exception. The decorator will not catch the exception. |
Hmm, good point, although the same concern (i.e. "Will this get cleaned up properly?") applies to pretty much any code using We get away with it because, 99.9%, of the time, management commands are run in their own process via |
I'll spend some time to test nested commands (ex: a command calling another command) and see if there are any problems. |
especially this one: from django.core import management
from django.core.management.commands import loaddata
management.call_command("flush", verbosity=0, interactive=False)
management.call_command("loaddata", "test_data", verbosity=0)
management.call_command(loaddata.Command(), "test_data", verbosity=0) # <-- here https://docs.djangoproject.com/en/4.2/ref/django-admin/#running-management-commands-from-your-code |
You could maybe do a similar trick to the way you've handled Celery task IDs:
|
The test i'll have to do is to see if the command_id is preserved. the expected behaviour would be like this:
|
and handling of |
I managed to do it: 2023-09-15T00:10:10.466616Z [info ] command_started [django_structlog.commands] command_id=f2a8c9a8-5aa3-4e22-b11c-f387449a34ed command_name=django_structlog_demo_project.users.example_command foo=bar
2023-09-15T00:10:10.467250Z [info ] my log [django_structlog_demo_project.users.management.commands.example_command] command_id=f2a8c9a8-5aa3-4e22-b11c-f387449a34ed
2023-09-15T00:10:10.468176Z [info ] command_started [django_structlog.commands] baz=2 command_id=57524ccb-a8eb-4d30-a989-4e83ffdca9c0 command_name=django_structlog_demo_project.users.example_nested_command parent_command_id=f2a8c9a8-5aa3-4e22-b11c-f387449a34ed
2023-09-15T00:10:10.468871Z [info ] my nested log [django_structlog_demo_project.users.management.commands.example_nested_command] command_id=57524ccb-a8eb-4d30-a989-4e83ffdca9c0 parent_command_id=f2a8c9a8-5aa3-4e22-b11c-f387449a34ed
2023-09-15T00:10:10.469418Z [info ] command_finished [django_structlog.commands] command_id=57524ccb-a8eb-4d30-a989-4e83ffdca9c0 parent_command_id=f2a8c9a8-5aa3-4e22-b11c-f387449a34ed
2023-09-15T00:10:10.469964Z [info ] my log 2 [django_structlog_demo_project.users.management.commands.example_command] command_id=f2a8c9a8-5aa3-4e22-b11c-f387449a34ed
2023-09-15T00:10:10.470585Z [info ] command_finished [django_structlog.commands] command_id=f2a8c9a8-5aa3-4e22-b11c-f387449a34ed I decided not to log the outcome since it can be anything. |
I made a PR to django-extensions in order to be sure post_command is called. |
@bcdickinson, in case you want to test it, I released Ex: pip install django-structlog[commands]==6.0.0.dev2 You can also take a look at django-structlog's command documentation |
could be used along with django-extensions's command signals
Something like this:
The text was updated successfully, but these errors were encountered: