I want to use the logging module, but I'm having some trouble because it is outputting twice. I've read a lot of posts with people having the same issue and log.propagate = False or `log.handlers.pop()´ fixed it for them. This doesn't work for me.
I have a file called logger.py that looks like this:
import logging
def __init__():
log = logging.getLogger("output")
filelog = logging.FileHandler("output.log")
formatlog = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
filelog.setFormatter(formatlog)
log.addHandler(filelog)
log.setLevel(logging.INFO)
return log
So that I from multiple files can write:
import logger
log = logger.__init__()
But this is giving me issues. So I've seen several solutions, but I don't know how to incorporate it in multiple scripts without defining the logger in all of them.
output.logand stderr? In any case yes it looks like you should be settinglog.propagate = Falsein your function. Or (less likely) does each message appear twice inoutput.log?log.propagate = Falsewould guard against every message also being written by the root logger, if the root has a handler. If this is the only configuration you do, that'll never happen so you actually don't have to touchpropagate. But if you call logging.log(...), logging.debug(), ... or logging.critical(), they will "helpfully" create a root handler if there aren't any.