[英]How to do logging.DEBUG in django code?
In django we have settings.py
that defines the DEBUG
for the whole project. 在django中,我们有
settings.py
来定义整个项目的DEBUG
。
Now, My have debug level is independently configured in settings.py. 现在,我的调试级别是在settings.py中独立配置的。
How should I use logging.DEBUG ? 我应该如何使用logging.DEBUG?
Way1: WAY1:
if settings.DEBUG:
logging.debug("Debug message")
Way2: Way2:
# Without checking settings.DEBUG
logging.debug("Debug message")
What is a good practice ? 什么是好习惯?
I think we should use Way2 since logging level already decides - if the message will be logged or not. 我认为我们应该使用Way2,因为日志记录级别已经决定-是否记录消息。 But, some say that Way1 is a standard practice.
但是,有人说Way1是标准做法。
I think it's not a good thing to rely too much on a global setting such as DEBUG, which changes the whole behavior of your app. 我认为,过分依赖诸如DEBUG之类的全局设置不是一件好事,它会改变应用程序的整体行为。
What if you want to audit code and log stuff in production ? 如果您要审核代码并将日志记录在生产中怎么办? You're not going to turn DEBUG to true to do this, are you ?
您不会将DEBUG设置为true,是吗? You'd rather tone down your log filter.
您宁愿调低日志过滤器的音量。
On a more stylistic point of view, it makes little sense and is not very pythonistic to have 2 settings (DEBUG and log level) affect a single behavior. 从更时尚的角度来看,让2个设置(DEBUG和日志级别)影响单个行为没有什么意义,也不是Python风格。
Long answer short: my opinion is that method 2 is superior, technically and stylisticly speaking. 长话短说:我认为方法2在技术和风格上都比较好。
The second method is fine, and in fact i use it all the time. 第二种方法很好,实际上我一直都在使用它。
The only reason I am putting an answer here is because we in-fact did something like (1) in a work project a few years back, it turned out that although we were not logging anything at debug level in production to a file the cost of creating the debug message was in itself quite expensive and impacting performance. 我在这里给出答案的唯一原因是因为几年前我们实际上在一个工作项目中做了类似(1)的事情,但事实证明,尽管我们没有在生产中将调试级别的任何内容记录到文件中创建调试消息本身成本很高,并且会影响性能。
ie 即
The project was a pretty big calculation farm where every ounce of performance mattered, this hasn't been the case for me ever since and might not be the case for you, but hey... i just thought i would mention it. 该项目是一个相当大的计算场,每盎司性能都很重要,从那以后我就没有这种情况了,对您而言可能就不是这种情况了,但是……我只是想提一提。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.