I want to use the logging module instead of printing for debugging information and documentation. The goal is to print to the console with the DEBUG level and register in the file with the INFO level.
I read a lot of documentation, cookbook and other lessons in the registration module, but could not understand how I can use it the way I want it. (I'm on python25)
I want to have the names of the modules in which the logs are written in my log file.
The documentation says that I should use logger = logging.getLogger(__name__) , but how to declare the registrars used in classes in other modules / packages, so they use the same handlers as the main registrar? To recognize the βparentβ, I can use logger = logging.getLogger(parent.child) , but where do I know who called the class / method?
The example below shows my problem, if I run this, the output will only contain __main__ logs and ignore the logs in Class
This is my main file:
# main.py import logging from module import Class logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG)
Module:
# module.py imnport logging class Class: def __init__(self): self.logger = logging.getLogger(__name__)
Console output :
- __main__ - INFO : Script starts - __main__ - INFO : calling class Class - __main__ - INFO : calling c.do_something() - __main__ - INFO : calling c.try_something() No handlers could be found for logger "module"
In addition: is there a way to get the names of the modules that were recorded in my journal without declaring a new registrar in each class, as described above? Also, as in this case, I need to go self.logger.info() every time I want to write something. I would prefer to use logging.info() or logger.info() in all my code.
Is a global journal perhaps the right answer? But then I wonβt get modules where errors occur in the logs ...
And my last question is: Is this a python? Or is there a better recommendation to do such things right.
python namespaces logging
uloco
source share