16👍
You can use logging.disable
– the following ensures logging.INFO
and below are disabled:
logging.disable(logging.INFO)
From the documentation:
Its effect is to disable all logging calls of severity lvl and below, so that if you call it with a value of INFO, then all INFO and DEBUG events would be discarded, whereas those of severity WARNING and above would be processed according to the logger’s effective level.
To undo it later, you can call:
logging.disable(logging.NOTSET)
3👍
I just ran into something similar, but I went in a different direction. I ended up with…
import logging
logging.root.setLevel(logging.DEBUG)
Python loggers work hierarchically, so setting it on the root logger affects all downstream loggers inheriting from it.
In a few other examples, the code, logging.getLogger()
, also returns the root logger, so the code here is essentially the same as those examples.
With knowledge of the hierarchical nature of loggers, the new information here is that you don’t need to set the level globally, on the root. You can be selective while still affecting multiple modules/loggers.
If you use the code log = logging.getLogger(__name__)
to retrieve your logger in each module, and if you configure your logger inside the __init__.py
file of a package, you can set the level on that specific logger, affecting all loggers inside that package.
- package_a
- __init__.py
- module_a.py
- module_b.py
- package_b
- __init__.py
- module_c.py
- module_d.py
Say you have packages and modules set up, as is shown above.
import logging
logging.getLogger('package_a').setLevel(logging.DEBUG)
Running this code will make it so module_a.py and module_b.py are now configured to DEBUG level, while module_c and module_d remain unaltered.
- How to store an integer leaded by zeros in django
- How to append extra data to the existing serializer in django
2👍
As an alternative to using the disable function, you can override the default level setting of WARNING
as follows:
import logging
logging.getLogger().setLevel(logging.INFO) # choose your level here
I wouldn’t call this a global solution per-se, as it requires a file-level declaration, but it’s useful IMO for simple debugging during development before a more cohesive project hierarchy has been established.
- Django limit_choices_to on user group
- How to call asynchronous function in Django?
- Limit Maximum Choices of ManyToManyField