Reputation: 540
I know this question has been asked like a million times before, but I just don't know why this:
import logging
from mylib import MyClass
hdlr = logging.FileHandler("MyLog.log", encoding="utf-16", mode="w")
hdlr.setFormatter(
logging.Formatter("[%(levelname)s] %(name)s <%(module)s.%(funcName)s> %(message)s")
)
hdlr.setLevel(logging.DEBUG)
myobj = MyClass(handlers=[hdlr])
where myclass.py
which defines MyClass
looks like this:
import logging
logger = logging.getLogger("MyClass")
class MyClass:
def __init__(self, handlers):
for hdlr in handlers:
logging.root.addHandler(hdlr)
This only outputs WARNING
messages in the file.
I read this question and I verified that I am not doing any logging before adding the handlers to root logger.
However replacing this line,
hdlr.setLevel(logging.DEBUG)
to this:
logging.root.setLevel(logging.DEBUG)
works, when it apparently (jump to the EDIT) shouldn't?
Some background info: MyClass
is a library I am working on. Its root __init__.py
contains no logging code, (although I think libraries should have some logging related code in that file).
Upvotes: 1
Views: 1102
Reputation: 4999
It doesn't matter what the level of the handler is, if the message never makes it to the handler. By default, the root logger level is WARN; it won't dispatch INFO or DEBUG messages to any handlers. When you set the root logger to DEBUG, it will send DEBUG messages to handler(s), which then can decide what to do with them based on their level settings.
Related to logging | error() gets sent to FileHandler... but not other Logging Levels
Upvotes: 1