简体   繁体   English

如何在Django代码中进行logging.DEBUG?

[英]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

  • (1) In production the debug level message is not created at all, just a boolean check instead. (1)在生产中,根本不会创建调试级别消息,而只是创建布尔检查。
  • (2) In production the debug messages are created and propagated but just not logged into a file (well if that is in fact how you have setup your logging). (2)在生产中,调试消息是创建并传播的,但是没有记录到文件中(如果实际上是您设置记录日志的方式,那么会记录)。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM