django-rich-logging icon indicating copy to clipboard operation
django-rich-logging copied to clipboard

Indicate if a request is AJAX

Open adamghill opened this issue 3 years ago • 6 comments

adamghill avatar Feb 13 '22 13:02 adamghill

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},
            ]
        },
    },
}

itsthejoker avatar Jul 21 '22 19:07 itsthejoker

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?

adamghill avatar Jul 22 '22 00:07 adamghill

@sjbitcode Since you just created https://github.com/adamghill/django-rich-logging/pull/5, what do you think about this idea?

adamghill avatar Jul 22 '22 00:07 adamghill

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

sjbitcode avatar Jul 22 '22 02:07 sjbitcode

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).

adamghill avatar Aug 14 '22 01:08 adamghill

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.

adamghill avatar Aug 14 '22 13:08 adamghill