django-rich-logging
django-rich-logging copied to clipboard
Indicate if a request is AJAX
Maybe it would be possible to arbitrarily add columns, maybe with a declarable function to get information for the logging? I suggest because I don't particularly care about AJAX, but I would like to know if something's from HTMX using https://github.com/adamchainz/django-htmx. I'm spitballing here, but if we're looking for arbitrary information and if this doesn't sound completely ridiculous it could look like:
def logging_is_htmx(request, *args, **kwargs) -> str:
return "✔️" if request.htmx else ""
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"django_rich_logging": {
"class": "django_rich_logging.logging.DjangoRequestHandler",
"level": "INFO",
"extra_columns": [
{"title": "HTMX", "function": logging_is_htmx},
]
},
},
}
This is a neat idea! I wonder if all of the columns could be configurable, but there is a default set of columns if none are specified. Or is that overkill?
@sjbitcode Since you just created https://github.com/adamghill/django-rich-logging/pull/5, what do you think about this idea?
I wonder if all of the columns could be configurable, but there is a default set of columns if none are specified.
That's a great idea! The configurability aspect would be really nice 👍
I don't think its overkill. You could support both column replacing and column extending...
- no columns specified ➡️ default columns
-
columns
specified ➡️ replace default columns -
extra_columns
specified ➡️ extend default columns
I have an initial PR for configurable columns, but it doesn't handle arbitrary functions. I'll need to see how doable that is (not sure if I have access to the actual request object when logging).
I dug into this little bit more and django.server
does send along a request
when messages get logged, but unfortunately it is a socket.socket
. I also found this https://code.djangoproject.com/ticket/27234 from 6 years ago, but it isn't especially helpful!
I spent a little bit of time trying to determine if Django's HttpRequest
was available, but I couldn't figure it out.