在 Django App 的控制台输出中显示警告
Display warning in console output in Django App
我想向旧的基于函数的视图添加警告,提醒用户我们正在转向基于 class 的视图。我在 Django 中一直看到这些警告,例如 RemovedInDjango19Warning
。真的很酷。
def old_view(request, *args, **kwargs):
print('I can see this message in my terminal output!')
warnings.warn("But not this message.", DeprecationWarning, stacklevel=2)
view = NewClassBasedView.as_view()
return view(request, *args, **kwargs)
我在终端输出中看到了 print
语句,但没有看到警告本身。我在终端中收到来自 Django 的其他警告(就像前面提到的 RemovedInDjango19Warning
但不是这个。
我错过了什么吗?我是否必须以某种方式在导入特定级别打开警告(即使它广泛适用于 Django)?
假设您使用的是 Django 1.8 并且没有日志记录配置,您可以从 Django documentation:
Django uses Python’s builtin logging module to perform system logging. The usage of this module is discussed in detail in Python’s own documentation.
所以你可以这样做:
# import the logging library
import logging
# Get an instance of a logger
logger = logging.getLogger(__name__)
def my_view(request, arg1, arg):
...
if bad_mojo:
# Log an error message
logger.error('Something went wrong!')
logging模块很好用,功能太多,建议你看一下Python logging documentation,比如你可以做:
FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'
logging.basicConfig(format=FORMAT)
d = {'clientip': '192.168.0.1', 'user': 'fbloggs'}
logger = logging.getLogger('tcpserver')
logger.warning('Protocol problem: %s', 'connection reset', extra=d)
输出到:
2006-02-08 22:20:02,165 192.168.0.1 fbloggs Protocol problem: connection reset
格式化您的所有消息并向您的用户发送非常棒的消息。
如果您想使用警告模块,您必须知道 Python 默认情况下不会显示应用程序引发的所有警告。有两种方法可以改变这种行为:
By parameters: 你必须用-Wd
参数调用python来加载默认过滤器,所以你可以这样做python -Wd manage.py runserver
调用测试服务器。
通过程序:您只需调用一次warnings.simplefilter('default')
函数。您可以从任何地方调用此函数,但您必须确保在调用 warnings.warn
之前执行此行,在我的测试中,我将其放在 settings.py
文件的开头,但我没有确定那是最好的地方。项目包的 __init__.py
文件将是一个不错的地方。
我想向旧的基于函数的视图添加警告,提醒用户我们正在转向基于 class 的视图。我在 Django 中一直看到这些警告,例如 RemovedInDjango19Warning
。真的很酷。
def old_view(request, *args, **kwargs):
print('I can see this message in my terminal output!')
warnings.warn("But not this message.", DeprecationWarning, stacklevel=2)
view = NewClassBasedView.as_view()
return view(request, *args, **kwargs)
我在终端输出中看到了 print
语句,但没有看到警告本身。我在终端中收到来自 Django 的其他警告(就像前面提到的 RemovedInDjango19Warning
但不是这个。
我错过了什么吗?我是否必须以某种方式在导入特定级别打开警告(即使它广泛适用于 Django)?
假设您使用的是 Django 1.8 并且没有日志记录配置,您可以从 Django documentation:
Django uses Python’s builtin logging module to perform system logging. The usage of this module is discussed in detail in Python’s own documentation.
所以你可以这样做:
# import the logging library
import logging
# Get an instance of a logger
logger = logging.getLogger(__name__)
def my_view(request, arg1, arg):
...
if bad_mojo:
# Log an error message
logger.error('Something went wrong!')
logging模块很好用,功能太多,建议你看一下Python logging documentation,比如你可以做:
FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'
logging.basicConfig(format=FORMAT)
d = {'clientip': '192.168.0.1', 'user': 'fbloggs'}
logger = logging.getLogger('tcpserver')
logger.warning('Protocol problem: %s', 'connection reset', extra=d)
输出到:
2006-02-08 22:20:02,165 192.168.0.1 fbloggs Protocol problem: connection reset
格式化您的所有消息并向您的用户发送非常棒的消息。
如果您想使用警告模块,您必须知道 Python 默认情况下不会显示应用程序引发的所有警告。有两种方法可以改变这种行为:
By parameters: 你必须用
-Wd
参数调用python来加载默认过滤器,所以你可以这样做python -Wd manage.py runserver
调用测试服务器。通过程序:您只需调用一次
warnings.simplefilter('default')
函数。您可以从任何地方调用此函数,但您必须确保在调用warnings.warn
之前执行此行,在我的测试中,我将其放在settings.py
文件的开头,但我没有确定那是最好的地方。项目包的__init__.py
文件将是一个不错的地方。