A
Andrea Crotti
I think is simple but I can't get it to work as I wish.
Suppose I have a big application, my idea is that the running
script sets a global logging level and then all the imported modules
would act consequently.
In my codebase, however, unless I set the level for each of the loggers
I don't get the debug messages, not really sure why yet.
To check if I understood,
logging.getLogger() returns the root logger, and every other logger
should inherit its settings, is that correct?
What happens if there is another logging.getLogger() somewhere else
in the code after the initialization? Does that become the root logger
overriding the old one?
The example below instead works as expected, so maybe is something
with my codebase...
# m1.py
import logging
logging.basicConfig()
# if no name is specified return the root logger, that's how it works
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
class ToLogEvents(object):
def __init__(self):
self.logger = logging.getLogger('ToLogEvents')
def important(self):
self.logger.warning("This is an important message")
self.logger.debug("debug message")
if __name__ == '__main__':
logger.debug("debug from m1")
from m2 import logging_function
logging_function()
t = ToLogEvents()
t.important()
# m2.py
import logging
logger = logging.getLogger(__name__)
def logging_function():
logger.debug("debug message")
logger.info("info message")
Suppose I have a big application, my idea is that the running
script sets a global logging level and then all the imported modules
would act consequently.
In my codebase, however, unless I set the level for each of the loggers
I don't get the debug messages, not really sure why yet.
To check if I understood,
logging.getLogger() returns the root logger, and every other logger
should inherit its settings, is that correct?
What happens if there is another logging.getLogger() somewhere else
in the code after the initialization? Does that become the root logger
overriding the old one?
The example below instead works as expected, so maybe is something
with my codebase...
# m1.py
import logging
logging.basicConfig()
# if no name is specified return the root logger, that's how it works
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
class ToLogEvents(object):
def __init__(self):
self.logger = logging.getLogger('ToLogEvents')
def important(self):
self.logger.warning("This is an important message")
self.logger.debug("debug message")
if __name__ == '__main__':
logger.debug("debug from m1")
from m2 import logging_function
logging_function()
t = ToLogEvents()
t.important()
# m2.py
import logging
logger = logging.getLogger(__name__)
def logging_function():
logger.debug("debug message")
logger.info("info message")