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).