Django 使用Python's Standard Logging Module;可能应该含蓄地回答他们如何实现定制
记录行为。 Python 具有出色的日志记录 documentation、cookbooks 和 how-tos。
问。如何仅将特定于应用程序的日志发送给其维护者?
下面的示例不会详细介绍 Loggers、LogRecords、Formatters、Handlers 或 Django 的日志记录行为。
LogRecords 被传播到它们的父目录。开发者 i.e __author__ 维护 __package__
需要在settings.py 文件上写一个project_level 记录器。假设tutorial 是开发人员正在维护的包。
.
├── manage.py
├── requirements.txt
└── tutorial
├── asgi.py
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
1 directory, 7 files
为了简洁起见,专注于处理程序、django 的记录器和过滤器。
├── settings.py
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
"""
another alternate handler inside your package.
Where custom behavior is implemented. key, value pair
dictionary is given. Option to pass extra parameter is the
handler class needs one. """
|. # the logRecords will propapagte up
|. # to it's parent dirs. Your
|. # project root where you'll handle it.
|...project/
|... __init__.py
|... logging.py
|... class NinjalFilter:
|... raise NotImplementedError
'Ninja': {
'()': 'project.logging.NinjalFilter',
'key': 'value',
},
},
'handler': {
'mail_admins': {
'level': 'ERROR', # 4XX (ERROR) or 5XX (Warning) Your Wish
'class': 'django.utils.log.AdminEmailHandler',
'filters': ['Ninja']
},
},
'logger': {
"""
Django introduced loggers classes Not in the standard Library.
https://docs.djangoproject.com/en/4.0/ref/logging/#default-logging-configuration
"""
'YouCustomLogger': {
'handlers': ['mail_admins', 'NinjaProjectMaintainerAdmins'],
'level': 'INFO', # Again 4XX (ERROR) or 5XX (Warning) Your Wish
'filters': ['Ninja']
}
}
}
CustomNinjaLogger 似乎将您的特殊Ninja 过滤后的日志记录传递给处理程序main_admins 和自定义NinjaProjectMaintainerAdminsHandler。请注意,如果 builtin mail_admins 完成这项工作,您可能不需要自定义 handler、NinjaProjectMaintainerAdminHandler。
我正在开发一个开源异步记录器——它使记录更容易。我可以使用功能请求、建议。祝你有美好的一天。希望答案有所帮助。