Getting Django to log errors to a file and everything else to the console
NickName:TropicalRaisel Ask DateTime:2021-08-20T08:03:14

Getting Django to log errors to a file and everything else to the console

Given the following logging config:

settings.py

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "filters": {
        "require_debug_false": {"()": "django.utils.log.RequireDebugFalse",},
        "require_debug_true": {"()": "django.utils.log.RequireDebugTrue",},
    },
    "formatters": {
        "django.server": {
            "()": "django.utils.log.ServerFormatter",
            "format": "[{server_time}] {message}",
            "style": "{",
        }
    },
    "handlers": {
        "console": {"level": "INFO", "filters": ["require_debug_true"], "class": "logging.StreamHandler",},
        "file": {
            "level": "ERROR",
            "filters": ["require_debug_false"],
            "class": "logging.FileHandler",
            "filename": BASE_DIR / "debug.log",
        },
        "django.server": {"level": "INFO", "class": "logging.StreamHandler", "formatter": "django.server",},
        "mail_admins": {
            "level": "ERROR",
            "filters": ["require_debug_false"],
            "class": "django.utils.log.AdminEmailHandler",
        },
    },
    "loggers": {
        "django": {"handlers": ["file", "console"], "level": "INFO",},
        "django.server": {"handlers": ["django.server"], "level": "INFO", "propagate": False,},
    },
}

Present Output

  • Console logs information- and warning-level alerts to the console
  • Log file debug.log is created
  • Compilation errors are not printed to the log file in production

What about this config does not allow errors to be sent to the log file?

Copyright Notice:Content Author:「TropicalRaisel」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/68855648/getting-django-to-log-errors-to-a-file-and-everything-else-to-the-console

More about “Getting Django to log errors to a file and everything else to the console” related questions

Getting Django to log errors to a file and everything else to the console

Given the following logging config: settings.py LOGGING = { "version": 1, "disable_existing_loggers": False, "filters": { "require_debug_false...

Show Detail

Log all errors to console or file on Django site

How can I get Django 1.0 to write all errors to the console or a log file when running runserver in debug mode? I've tried using a middleware class with process_exception function as described i...

Show Detail

Log all errors to console or file on Django site

How can I get Django 1.0 to write all errors to the console or a log file when running runserver in debug mode? I've tried using a middleware class with process_exception function as described i...

Show Detail

How to log Django warnings and errors to log file in Production?

What I want to achieve is that warning and errors that happen in production (i.e. DEBUG=False) are logged into to a rotating log file. I tried this LOGGING = { 'version': 1, '

Show Detail

django - outputting console to log file

I'm got the following logging config - LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'file': { 'level': '

Show Detail

Django "./manage.py runserver" log to file instead of console

Running python manage.py runserver will initiate Django's development server and all logs are printed to the console. I need to write the logs in a django.log file instead of console. Django logg...

Show Detail

Maintaining log.django file

I have the standard django logging config in my settings.py: LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'console': { '

Show Detail

Making curl send errors to stderr and everything else to stdout

Is there a way to tell curl to output errors to stderr, and everything else to stdout? The reason is that I am using curl from the command line (actually a cronjob) to upload a file to an FTP site...

Show Detail

DEBUG = False causing 500 errors and unable to force logging everything to console in Django

I am trying to troubleshoot a 500 error, as whenever I set DEBUG = False in Django, I receive a 500 error. I attempted to set ALLOWED_HOSTS = ['*'] I attempted to setup LOGGING to debug and fo...

Show Detail

Why does console.log stop errors in javascript Library?

I have been toying with creating a small library for a web app I am working on. In creating this library, I can enter a log statement at the top of the script, and everything below works fine. Howe...

Show Detail