我知道這已經是一個解決的答案,但根據django> = 1.3,有一個新的日誌記錄設置。
從舊到新不是自動的,所以我想我會在這裏寫下來。
當然結賬the django doc一些更多。
這是基本的conf,默認情況下使用Django管理員的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,這決定了以下塊:
我通常至少這一點:
換算成:
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正確的過濾器,這樣,在默認情況下,電子郵件不會在調試是真發。
你應該添加過濾器:
'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,
}
否則django.core.handers.base.handle_uncaught_exception
不傳遞錯誤,如果設置了 'django.request' 記錄儀.DEBUG是真的。
如果你不這樣做在Django 1.5,你會得到一個
DeprecationWarning:你有沒有關於「mail_admins」日誌處理程序定義的過濾器:添加隱調試假只能過濾
但事情仍然會正常工作在Django 1.4和Django 1.5中。
**結束編輯**
那的conf強烈由Django的文檔樣本的conf的啓發,但添加日誌文件的一部分。
我也經常做到以下幾點:
LOG_LEVEL = 'DEBUG' if DEBUG else 'INFO'
...
'level': LOG_LEVEL
...
然後在我的Python代碼,我總是在增加的情況下沒有日誌記錄的conf是任何定義的NullHandler。這避免了沒有指定Handler的警告。對於庫特別有用的不一定只調用在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.')
希望這有助於!
您可能會發現以下屏幕視頻有用 - http://ericholscher.com/blog/2008/aug/29/screencast-2-logging-fun-and-profit/。另外,Simon Willison提出了更好的登錄Django的支持(參見http://simonwillison.net/2009/Sep/28/ponies/)。 – 2009-10-21 05:20:00
@Dominic Rodger - 您可以在Django中靈活地記錄應用程序,Simon的建議主要是爲了便於在Django內部進行日誌記錄。Python正在開展工作,將基於字典的配置添加到Python日誌記錄中,Django可能從中受益。 – 2009-10-21 06:05:43