如何在Django日志文件中记录Python警告? [英] How to log Python warnings in a Django log file?
问题描述
我有一个Django应用程序,正在从v1.8迁移到v1.10。在执行此工作的过程中,我通过以下方式运行了应用程序:
I have a Django application that I am migrating from v1.8 to v1.10. In the process of doing this work, I ran my application via:
python -Wall manage.py runserver
这样做会在控制台中显示许多Python警告。我想在我的Django应用程序日志中显示这些警告,以便稍后进行检查。我以为我的应用程序的日志处理程序会捕获这些警告,但事实并非如此。日志处理程序如下所示(取自settings.py):
Doing so causes a number of Python warnings to appear in my console. I'd like to have these warnings show up in my Django application log, so I can examine them later. I thought my application's log handler would catch these warnings, but it doesn't. The log handler looks like the following (as taken from settings.py):
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
'datefmt': "%d/%b/%Y %H:%M:%S"
},
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
'file': {
'level': 'WARNING',
'class': 'logging.FileHandler',
'filename': 'myapp.log',
'formatter': 'verbose'
},
},
'loggers': {
'django': {
'handlers': ['file'],
'propagate': True,
'level': 'WARNING',
},
'myapp': {
'handlers': ['file'],
'level': 'WARNING',
},
}
}
如何我在Django日志中捕获了Python警告(使用 -Wall
)供以后检查?
How can I capture Python warnings (with -Wall
) in my Django log for examination later?
推荐答案
python -Wall manage.py< command>
中显示的警告是使用警告模块生成的。在Python2.7日志记录模块文档中,它们有一节显示如何将来自警告模块的消息集成到日志记录中。
The warnings shown in python -Wall manage.py <command>
are generated using warnings module. In Python2.7 logging module documentation, they have a section showing how to integrate messages from warnings module to logging.
将这些消息添加到设置文件中。
Add these to your settings file.
import logging
logging.captureWarnings(True)
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
'datefmt': "%d/%b/%Y %H:%M:%S"
},
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
'file': {
'level': 'WARNING',
'class': 'logging.FileHandler',
'filename': 'myapp.log',
'formatter': 'verbose'
},
},
'loggers': {
'django': {
'handlers': ['file'],
'propagate': True,
'level': 'WARNING',
},
'myapp': {
'handlers': ['file'],
'level': 'WARNING',
},
'py.warnings': {
'handlers': ['file'],
'level': 'WARNING',
'propagate': True
}
}
}
之后添加它,尝试运行任何 manage.py
命令,您会看到写在 myapp.log
文件中的警告
After adding it, try to run any manage.py
command and you can see the warnings written to your myapp.log
file.
此行指示日志记录以捕获py.warnings 警告:
This line instructs logging to capture the py.warnings warnings:
logging.captureWarnings(True)
此命令指示日志记录路由(在此示例中,是来自py.warnings记录器的 file 处理程序)警告:
This instructs logging to route (in this example to the file handler) warnings from the py.warnings logger:
'py.warnings': {
'handlers': ['file'],
'level': 'WARNING',
'propagate': True
}
这篇关于如何在Django日志文件中记录Python警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!