Tivix / django-cron

Write cron business logic as a Python class and let this app do the rest! It enables Django projects to schedule cron tasks, tracks their success / failures, manages contention (via a cache) etc. Basically takes care of all the boring work for you :-)
www.tivix.com
MIT License
900 stars 193 forks source link

Unsure how to log exceptions caught by django_cron #143

Closed dom-devel closed 2 years ago

dom-devel commented 6 years ago

I'm having trouble getting functions run through cron to log exceptions.

I know this module captures all exceptions, but I couldn't follow the part about logging to it's own error model.

Is there additional standard set-up required to log exceptions from django_cron that I'm missing?

Method used for running cron: python manage.py runcrons "data_load_api.cron.RawDataLoading" Server set-up:

Django logging: Everything get's logged straight to console and therefore output through the gunicorn supervisor logs.

The logging set-up is fairly basic. There's a custom filter which prevents logging for unit tests.

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "filters": {"testing": {"()": NotInTestingFilter}},
    "formatters": {
        "simple": {"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"}
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "level": "DEBUG",
            "filters": ["testing"],
            "formatter": "simple",
            "stream": "ext://sys.stdout",
        }
    },
    "loggers": {
        "": {
            "handlers": ["console"],
            "level": "INFO"
            # "propagate": False,
        },
    },
}

I tried adding an additional handler specifically for django_cron, in case I needed to override but that didn't change anything.

"loggers": {
        "": {
            "handlers": ["console"],
            "level": "INFO"
            # "propagate": False,
        },
        "django_cron": {
            "handlers": ["console"],
            "level": "INFO"
            # "propagate": False,
        },
    },

glotchimo commented 6 years ago

Why not just build your own model and extend the CronJobLog as a base class?