Vanilla Django launches ResourceWarning: "unclosed file" on registration

I have a problem setting up Django 1.8 / Python 3.4. At startup

python -Wall ./manage.py runserver

      

I am getting the following warning:

/lib/python3.4/logging/config.py:763: 
ResourceWarning: unclosed file <_io.TextIOWrapper name='/Users/furins/logs/test-project.log' mode='a' encoding='UTF-8'>
  for h in logger.handlers[:]:

      

these are the settings in settings.py

related to logging:

LOGGING_LEVEL = 'DEBUG'

LOG_DATE_FORMAT = '%d %b %Y %H:%M:%S'

LOG_FORMATTER = logging.Formatter(
    '%(asctime)s | %(levelname)-7s | %(name)s | %(message)s',
    datefmt=LOG_DATE_FORMAT)

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'formatters': {
        'standard': {
            'format': "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt': LOG_DATE_FORMAT
        },
    },
    'handlers': {
        'logfile': {
            'level': LOGGING_LEVEL,
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': PROJECT_DIR / 'logs/test-project.log',
            'maxBytes': 1024 * 1024 * 5,  # 5 MB
            'backupCount': 5,
            'formatter': 'standard',
        },
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'null': {
            'level': 'DEBUG',
            'class': 'django.utils.log.NullHandler',
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'standard'
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console', 'logfile'],
            'propagate': True,
            'level': 'DEBUG',
        },
        'django.db.backends': {
            'handlers': ['console', 'logfile'],
            'level': 'INFO',
            'propagate': False,
        },
        'django.request': {
            'handlers': ['console', 'mail_admins', 'logfile'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'eurogielle': {
            'handlers': ['console', 'logfile', 'mail_admins'],
            'level': LOGGING_LEVEL,
            'propagate': True
        },
        'eurogielle2': {
            'handlers': ['console', 'logfile', 'mail_admins'],
            'level': LOGGING_LEVEL,
            'propagate': True
        },
        'eurogielle_management': {
            'handlers': ['console', 'logfile', 'mail_admins'],
            'level': LOGGING_LEVEL,
            'propagate': True
        },
        'eurogielle_prodotti': {
            'handlers': ['console', 'logfile', 'mail_admins'],
            'level': LOGGING_LEVEL,
            'propagate': True
        },
    }
}

      

I've tried searching on Google and SO for a similar problem with no luck. I know how to reverse this warning, of course, but I would like to know if this warning refers to something that could leak resources in production and if there is a way to "close the file".

Has any Stackoverflow Django ninja had the same problem?

+3


source to share


1 answer


After further searching, I found a potentially related issue and a blog post that led me in the right direction.

It was really simple. I just needed to add 'delay': True

to the config:

    'logfile': {
        'level': LOGGING_LEVEL,
        'class': 'logging.handlers.RotatingFileHandler',
        'filename': PROJECT_DIR / 'logs/test-project.log',
        'maxBytes': 1024 * 1024 * 5,  # 5 MB
        'backupCount': 5,
        'formatter': 'standard',

        'delay': True  // <- this line solved the problem!

    },

      



and there was no warning! For more reference, the property is explained in the python docs :

If the delay is true, then opening the file is delayed until the first call to emit ().

+5


source







All Articles