0

In django we have settings.py that defines the DEBUG for the whole project.

Now, My have debug level is independently configured in settings.py.

How should I use logging.DEBUG ?

Way1:

if settings.DEBUG:
    logging.debug("Debug message")

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. But, some say that Way1 is a standard practice.

1
  • If you're using the logging module, it should be logging.debug() and not all caps. Commented Nov 9, 2011 at 6:26

2 Answers 2

4

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.

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 ? 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.

Long answer short: my opinion is that method 2 is superior, technically and stylisticly speaking.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

i.e.

  • (1) In production the debug level message is not created at all, just a boolean check instead.
  • (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).

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.

1 Comment

Thanks for sharing the experience !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.