I think the following code should print all messages but it only works if I call BasicConfig:
import logging
logger = logging.getLogger("log")
# logging.basicConfig(level=logging.DEBUG) SHOULD NOT BE NEEDED, YET IT IS.
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
formatter = logging.Formatter("%(levelname)s: %(filename)s(%(lineno)d)[%(name)s]: %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
print("This print test printed")
print(f"Enabled: {logger.isEnabledFor(logging.INFO)}")
logger.propagate = False
logger.debug("This debug printed")
logger.info("This info printed")
logger.warning("This warning printed")
logger.error("This error printed")
logger.critical("This critical printed")
I get that basicConfig controls the root logger, but I'm not using the root logger. Still I am getting this output:
This print test printed
Enabled: False
WARNING: scratch.py(15)[log]: This warning printed
ERROR: scratch.py(16)[log]: This error printed
CRITICAL: scratch.py(17)[log]: This critical printed
Shouldn't the code above had sidestepped the root logger and let my logger work at whatever level I want it to work at?