我知道这已经是一个已解决的答案,但是根据 django >= 1.3 有一个新的日志记录设置。
从旧到新不是自动的,所以我想我会在这里写下来。
当然还有更多信息,请查看the django doc。
这是基本配置,默认使用 django-admin createproject v1.3 创建 - 里程可能会随着最新的 django 版本而改变:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
}
}
}
此结构基于标准Python logging dictConfig,它规定了以下块:
我通常至少会这样做:
- 添加 .log 文件
- 配置我的应用程序以写入此日志
翻译成:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
'handlers': {
'null': {
'level':'DEBUG',
'class':'django.utils.log.NullHandler',
},
'console':{
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple'
},
# I always add this handler to facilitate separating loggings
'log_file':{
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': os.path.join(VAR_ROOT, 'logs/django.log'),
'maxBytes': '16777216', # 16megabytes
'formatter': 'verbose'
},
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler',
'include_html': True,
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
'apps': { # I keep all my of apps under 'apps' folder, but you can also add them one by one, and this depends on how your virtualenv/paths are set
'handlers': ['log_file'],
'level': 'INFO',
'propagate': True,
},
},
# you can also shortcut 'loggers' and just configure logging for EVERYTHING at once
'root': {
'handlers': ['console', 'mail_admins'],
'level': 'INFO'
},
}
编辑
见request exceptions are now always logged和Ticket #16288:
我更新了上面的示例 conf 以明确包含正确的 mail_admins 过滤器,因此默认情况下,当 debug 为 True 时不会发送电子邮件。
你应该添加一个过滤器:
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
并将其应用于 mail_admins 处理程序:
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler',
'include_html': True,
}
否则,如果 settings.DEBUG 为 True,django.core.handers.base.handle_uncaught_exception 不会将错误传递给“django.request”记录器。
如果你不在 Django 1.5 中这样做,你会得到一个
弃用警告:您没有在“mail_admins”日志记录处理程序上定义过滤器:添加隐式 debug-false-only 过滤器
但在 django 1.4 和 django 1.5 中,事情仍然可以正常工作。
** 结束编辑 **
该 conf 受到 django 文档中的示例 conf 的强烈启发,但添加了日志文件部分。
我也经常做以下事情:
LOG_LEVEL = 'DEBUG' if DEBUG else 'INFO'
...
'level': LOG_LEVEL
...
然后在我的 python 代码中,我总是添加一个 NullHandler 以防万一没有定义任何日志记录配置。这避免了未指定处理程序的警告。对于不一定只在 Django 中调用的库特别有用 (ref)
import logging
# Get an instance of a logger
logger = logging.getLogger(__name__)
class NullHandler(logging.Handler): #exists in python 3.1
def emit(self, record):
pass
nullhandler = logger.addHandler(NullHandler())
# here you can also add some local logger should you want: to stdout with streamhandler, or to a local file...
[...]
logger.warning('etc.etc.')
希望这会有所帮助!