Django logging only logs "root", no other handlers

Hi!

This is my Django logging config:

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "filters": {"require_debug_false": {"()": "django.utils.log.RequireDebugFalse"}},
    "formatters": {
        "verbose": {
            "format": "%(levelname)s %(asctime)s %(module)s "
            "%(process)d %(thread)d %(message)s"
        }
    },
    "handlers": {
        "mail_admins": {
            "level": "ERROR",
            "filters": ["require_debug_false"],
            "class": "django.utils.log.AdminEmailHandler",
            'include_html': True,
        },
        "console": {
            "level": "DEBUG",
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        },
        "file": {
            "class": "logging.FileHandler",
            "filename": os.path.join(APPS_DIR + "..", "app.log"),
        },
        'db_queries': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': os.path.join(APPS_DIR + "..", 'db_queries.log'),
        },
    },
    "root": {
        "level": "WARNING", 
        "handlers": ["file", "mail_admins"],
        "propagate": True,
    },
    "loggers": {
        "django.request": {
            "handlers": ["file", "mail_admins"],
            "level": "ERROR",
            "propagate": True,
        },
        "django.security.DisallowedHost": {
            "level": "ERROR",
            "handlers": ["file", "mail_admins"],
            "propagate": True,
        },
        "django": {
             "handlers": ["file"], 
             "level": "DEBUG", 
             "propagate": True,
        },
        'django.db.backends': {
            'level': 'DEBUG',
            'handlers': ['db_queries'],
            'propagate': False,
        },
    },
}

The “root” logger works, both for file and mails_admin, but the other loggers don’t seem to do anything. I would expect that at least the “django” and “django.db.backends” handlers would write something to file.

Any ideas what could be the cause?

PS, also on SO: python - Django logging only logs "root", no other handlers - Stack Overflow

Edit: django.db.backends only works when DEBUG=True apparently, so that explains that part. I still don’t understand why the “django” logger doesn’t work.

What is “APPS_DIR”?

This doesn’t appear right to me. I would think it should be:
os.path.join(APPS_DIR, "..", "app.log")

Yeah, that is better in case the APPS_DIR doesn’t have a trailing “/”, but it does :smile:

Problem remains.

Is this a development or production environment? Does the process running the server have the necessary permissions to that directory? (That’s why I was asking what the “APPS_DIR” setting was. If this is a production environment, you really don’t want your server to have “write” permissions in your application directory.)

It is a production environment (but still in development). Only the log file has write permissions for the www-data group. Is that still a concern? I could move the log file to /var/logs if that is more secure.

That’s definitely the more appropriate place for it. Remember that creating a new file requires write permissions to the directory in which that file is going to reside. (Creating a file means that you’re writing a new entry into that directory.)

1 Like

Yes you’re right. It might be related to what is going on as well. Thanks!