Django使用dictConfig时无法找到"logging"模块

4 投票
1 回答
4084 浏览
提问于 2025-04-16 22:59

我在设置中的LOGGING指令是这样设置的:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'default': {
            'format': '[%(asctime)s] %(levelname)s::(%(process)d %(thread)d)::%(module)s - %(message)s'
        },
    },
    'handlers': {
        'file_handler': {
            'level': 'DEBUG',
            'formatter':'default',
            'class': 'logging.TimedRotatingFileHandler',
            'filename':'Project_log',
            'when':'midnight',
            'interval':1
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['file_handler'],
            'level': 'DEBUG',
            'propagate': True,
        },
    }
}

处理程序的类被设置为logging.HandlerName,按照文档中的示例:https://docs.djangoproject.com/en/dev/topics/logging/

但是我收到了以下错误:

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/config.py", line 776, in dictConfig
    dictConfigClass(config).configure()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/config.py",     line 575, in configure
'%r: %s' % (name, e))
ValueError: Unable to configure handler 'file_handler': Cannot resolve 'logging.TimedRotatingFileHandler': No module named TimedRotatingFileHandler

1 个回答

10

你需要写上这段代码,

logging.handlers.TimedRotatingFileHandler

因为 TimedRotatingFileHandler 是日志处理工具包中的一部分。

撰写回答