我正在尝试在django设置文件中配置django日志记录,以便将django信息和我的应用程序的信息记录到自定义文件中以便于查看。这是我的日志记录配置:
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'console': {
# exact format is not important, this is the minimum information
'format': '%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
},
'file': {
# exact format is not important, this is the minimum information
'format': '%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
},
},
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'file',
'filename': 'logs/django_log.log',
'backupCount': 10, # keep at most 10 log files
'maxBytes': 5242880, # 5*1024*1024 bytes (5MB)
},
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'console',
},
},
'loggers': {
'django': {
'handlers': ['file', 'console'],
'level': 'INFO',
'propagate': True,
},
'py.warnings': {
'handlers': ['console'],
},
'my_application': {
'level': 'INFO',
'handlers': ['file', 'console'],
# required to avoid double logging with root logger
'propagate': False,
},
},
}
这在我的本地manage.py测试服务器上有效,同时显示了Django事件和我记录的事件,并以my_application
作为记录器名称进行了初始化。但是,在我的Web服务器上,创建了日志文件,并且奇怪的是,日志文件只偶尔填充了django WARNING
消息。因此,没有权限错误或无法访问日志文件。由于相同的配置可以在我的本地计算机上使用,因此该配置不会成为问题,并且显然只有INFO
级日志。
我的服务器设置来自以下指南:https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04,并将Gunicorn与Nginx一起用作反向代理。问题可能出在这些配置上吗?我很困惑。
此外,在哪里存储此django日志文件的最佳实践是什么?
还有一个相关的奖励问题:什么是最佳的免费/廉价最佳实践服务,如果记录了特定错误,该服务可以通知您?设置类似的东西似乎是一个好主意,但我认为django电子邮件发送程序不一定是最优雅或最好的。