Monday, 1 March 2021

Can I access the request object in a console error?

I am not very familiar with Django but have to make some changes to how it logs for a project. I am wanting to add information from the request object to the logs.

import logging

MODULE_LOGGER = logging.getLogger(__name__)

class IndexPage():

  def get(self, request, *args, **kwargs):
    
    MODULE_LOGGER.warning("log me!")
    # code...
    return self.render_to_response(context)

This then gets logged out by this class:

import json
from fluent import handler

class JsonFormatter(handler.FluentRecordFormatter):
    def format(self, record):
        data = super(JsonFormatter, self).format(record)
        return json.dumps(data)

And we have the following logger settings (which I don't really understand)

LOGGING = {
    "version": 1,
    "disable_existing_loggers": True,
    "root": {"level": "WARNING", "handlers": ["sentry", "json_console"]},
    "formatters": {
        "console": {"datefmt": syslog_date_fmt, "format": syslog_msg_format},
        "json_fmt": {
            "()": "out_project.logs.JsonFormatter",
            "format": {
                "level": "%(levelname)s",
                "hostname": "%(hostname)s",
                "name": "%(name)s",
                "where": "%(module)s.%(funcName)s",
                "stack_trace": "%(exc_text)s",
            },
        },
    },
    "filters": {
        "require_debug_false": {"()": "django.utils.log.RequireDebugFalse"},
        "file_logging": {"": ""},
    },
    "handlers": {
        "null": {
            "level": "DEBUG",
            "class": "logging.NullHandler",
        },
        "console": {
            "level": "DEBUG",
            "class": "logging.StreamHandler",
            "formatter": "console",
        },
        "sentry": {
            "level": "WARNING",
            "class": "raven.contrib.django.raven_compat.handlers.SentryHandler",
        },
        "json_console": {
            "level": "INFO",
            "class": "logging.StreamHandler",
            "formatter": "json_fmt",
        },
    },
    "loggers": {
        "raven": {
            "level": "INFO",
            "handlers": ["json_console"],
            "propagate": False,
        },
        "sentry.errors": {
            "level": "INFO",
            "handlers": ["json_console"],
            "propagate": False,
        },
        "out_project": {
            "level": "INFO",
            "propagate": True,
        },
    },
}

The Django docs talk about a django.request logger, however this seems to be when there is an issue with the request (say 4XX, 5XX error). I have tried adding this logger with its handlers being the json_console handler however it the request object doesn't appear on the record object.

Ideally I am looking to add data from the request object to the record, or for it to be automatically put there. I am assuming once it is there, I can then change how the log looks by updating the json_fmt.format property.

From what I understand I can add additional information to the individual logs, however the data I am wanting to get from the request needs to be added to every log that is done via the logger from logging.getLogger(__name__).

I am very new to how logging works in Django so apologies if I have explained or understood something incorrectly.



from Can I access the request object in a console error?

No comments:

Post a Comment