Django开发日志记录HttpResponses到dev服务器 [英] Django Development Logging HttpResponses to dev server

查看:214
本文介绍了Django开发日志记录HttpResponses到dev服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django创建一个API。每个视图都以JSON格式响应。我想将每个HttpResponse JSON记录到dev服务器输出。



到目前为止,我添加了一个处理程序:

 'console':{
'level':'DEBUG',
'class':'logging.StreamHandler',
}

然后添加了一个记录器:

 'to_console':{
'handlers':['console'],
'level':'DEBUG',
}

在我看来,我得到了记录器 logger = logging.getLogger('to_console')



并为每个json响应 logger.debug(json_str)



对于第一个视图,这很好。但是我想知道在将应用程序部署到生产中时是否可以关闭调试。看起来像 https://docs.djangoproject.com /en/dev/topics/logging/#django.utils.log.RequireDebugFalse 可以正常工作。但是,这导致我的代码散落在这些日志记录中。我从来没有需要像这样记录某些东西,所以我想知道最可维护的方法是什么。



处理开发日志记录的方法是什么?当代码生产时可以关闭还是有一些内置的功能或应用程序,我缺少自动将所有HttpResponse的日志记录到dev服务器?

解决方案

可以通过更改提供给日志记录的配置来关闭和启用日志功能。您可能会将logging语句视为乱抛垃圾的代码,但是一旦服务器运行在生产环境中,除了日志记录之外,您通常都会了解到内部正在发生的一切(通常)。



如果需要,您可以发送正在运行的服务器的POST与包含新配置的JSON数据,然后由处理POST请求的视图生效。但是建议将日志记录留在原位,因为当记录级别实际上没有输出时,它们相当便宜(如您所期望的那样,写入控制台/文件/套接字等的实际I / O占用大部分时间,但是如果级别设置得足够高,实际上输出的数量很少或没有)。



当开发和测试中的一切运行良好但生产中无法令人失望时,日志记录可以有时候是唯一可用的诊断工具。



我认为使用RequireDebugFalse对于您所描述的场景来说是非常合理的,但您也可以编写自己的过滤器与您的具体需求密切相关(撰写过滤器非常简单)。


I am creating an API using Django. Every view responds in JSON. I would like to log each HttpResponse JSON to the dev servers output.

So far I have added a handler:

 'console': {
        'level':'DEBUG',
        'class':'logging.StreamHandler',
 }   

and then added a logger:

'to_console': {
        'handlers': ['console'],
        'level': 'DEBUG',
    }   

In my view I get the logger logger = logging.getLogger('to_console')

and for each json response logger.debug(json_str)

For the first view this was fine. But I'm wondering is it possible to turn debug off when I deploy the app to production. It looks like https://docs.djangoproject.com/en/dev/topics/logging/#django.utils.log.RequireDebugFalse could work. But then that leads my code littered with these logging statements. I have never needed to log somethign like this so I am wondering what the most maintainable way to handle it is.

What is the correct way to handle development logging so that it can be "turned-off" when code is on production? Or is there some sort of built in functionality or app that I am missing that automatically logs all HttpResponse's to the dev server?

解决方案

Logging functionality can be turned off and on by varying the configuration you provide to logging. You might think of the logging statements as "littering" your code, but once the server's running in production, you have (in general) very few ways of knowing what's going on inside, other than logging.

If necessary, you can send a running server a POST with JSON data containing a new configuration, which then gets put into effect by the view handling the POST request. But it's advisable to leave the logging statements in place, as they are quite cheap when logging levels are such that nothing is actually output (as you might expect, the actual I/O of writing to console/file/socket etc. takes most of the time, but if levels are set high enough, little or nothing is actually output).

When everything runs fine in development and test but unaccountably fails in production, logging can sometimes be the only diagnostic tool available.

I think the use of RequireDebugFalse is quite reasonable for the scenario you describe, but you can also write your own filter which fits in more closely with your specific needs (writing Filters is quite easy to do).

这篇关于Django开发日志记录HttpResponses到dev服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆