简体   繁体   中英

Django: Setting LOGGER doesn't work

I'm having trouble with Django's logging system. My goal is to log all types of logs to the files, and only log to the console if DEBUG is set to True (which it is right now)

As it is, the log files remain blank (although they are created by Django). When I start the server (using python3 manage.py runserver ) I see the following in the shell:

Warning!
Error!

... (normal server stuff)

But nothing is written to any of the log files.

And if I un-comment logging.error('== == START LOGGER == ==') and logr.setLevel('DEBUG') in __init__.py , then the shell reads:

ERROR:root:== == START LOGGER == ==
DEBUG:project:Debug!
INFO:project:Info!
WARNING:project:Warning!
ERROR:project:Error!

... (normal server stuff)

And django.log displays:

DEBUG (0.000) SET SQL_AUTO_IS_NULL = 0; args=None
DEBUG (0.000) SET SQL_AUTO_IS_NULL = 0; args=None
DEBUG (0.001) SHOW FULL TABLES; args=None
DEBUG (0.000) SELECT `django_migrations`.`app`, `django_migrations`.`name` FROM `django_migrations`; args=()

But all other logs remain blank.

Here is my project app

__init__.py

import logging

#logging.error('== == START LOGGER == ==')
logr = logging.getLogger('project')
#logr.setLevel('DEBUG')

logr.debug('Debug!')
logr.info('Info!')
logr.warning('Warning!')
logr.error('Error!')

Here are my LOGGING settings

settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
        'simpler': {
            'format': '%(message)s'
        },
    },
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'handlers': {
        'default':{
            'level': 'WARNING',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': 'logs/err.log',
            'maxBytes': 5242880, # 5MB file size
            'encoding': 'utf-8',
            'backupCount': 5,
            'formatter': 'simple',
        },
        'debug':{
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': 'logs/debug.log',
            'maxBytes': 5242880, # 5MB file size
            'encoding': 'utf-8',
            'backupCount': 5,
            'formatter': 'simple',
        },
        'django':{
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': 'logs/django.log',
            'maxBytes': 5242880, # 5MB file size
            'encoding': 'utf-8',
            'backupCount': 5,
            'formatter': 'simple',
        },
        'mail':{
            'level': 'WARNING',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': 'logs/mail.log',
            'maxBytes': 5242880, # 5MB file size
            'encoding': 'utf-8',
            'backupCount': 5,
            'formatter': 'simple',
        },
        'console': {
            'level': 'DEBUG',
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
            'formatter': 'simpler',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['django'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'django.db.backends': {
            'handlers': ['django'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'django.request': {
            'handlers': ['mail'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'project': {
            'handlers': ['default', 'debug', 'console'],
            'level': 'DEBUG',
            'propagate': False,
        },
    },
}

Why does it show DEBUG and INFO when I write logging.error('== == START LOGGER == ==') and logr.setLevel('DEBUG') ?? Why don't the log files get written to? I've searched through similar questions and their answers for hours with no success. Thanks.

Edit: Things I've tried:

Changing 'disable_existing_loggers' to False

Changing 'level': 'DEBUG', to 'level': 'NOTSET', and 'level': 'ERROR',

Removing all but necessary settings

Changing 'propagate' to True

Removing 'console' from 'handlers' (which does absolutely nothing)

I don't have enough points to add a comment, so I'll mention in here that if your logging format uses %(name)s then a useful extension to the above answer is, in the files where you have:

logr = logging.getLogger('project')

you could use:

logr = logging.getLogger('project.%s' % __name__)

This would then mean your project logger reports the full App name, eg. 'project.polls.views'

Your __init__.py file is called before settings.py

Move your code from __init__.py to any other file, which will be called after settings.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM