加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – Vanilla Django在日志记录中投射了一个ResourceWarnin

发布时间:2020-12-20 11:42:23 所属栏目:Python 来源:网络整理
导读:我的Django 1.8 / Python 3.4设置存在问题. 跑步时 python -Wall ./manage.py runserver 我收到以下警告: /lib/python3.4/logging/config.py:763: ResourceWarning: unclosed file _io.TextIOWrapper name='/Users/furins/logs/test-project.log' mode='a'
我的Django 1.8 / Python 3.4设置存在问题.
跑步时

python -Wall ./manage.py runserver

我收到以下警告:

/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[:]:

这些是与日志记录相关的settings.py中的设置:

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','level': 'INFO','propagate': False,'django.request': {
            'handlers': ['console','mail_admins','eurogielle': {
            'handlers': ['console','logfile','mail_admins'],'level': LOGGING_LEVEL,'propagate': True
        },'eurogielle2': {
            'handlers': ['console','eurogielle_management': {
            'handlers': ['console','eurogielle_prodotti': {
            'handlers': ['console',}
}

我试图在Google和SO上搜索类似的问题,但没有运气.当然,我知道如何沉默这个警告,但我想知道这个警告是否指的是可能泄漏生产资源的东西以及是否有办法“关闭文件”.

任何Stackoverflow Django忍者都有同样的问题吗?

解决方法

经过进一步搜索后,我发现了 a potentially related issue和 a blog post,这使我朝着正确的方向前进.

确实很简单.我只需要添加’延迟’:配置为True:

'logfile': {
        'level': LOGGING_LEVEL,# 5 MB
        'backupCount': 5,'delay': True  // <- this line solved the problem!

    },

并且警告消失了!
为了进一步参考,该财产被解释为in python docs:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读