I'm trying to put together a logger using Python. I'm working in 2.6 so I can't use the new dictionary style method and instead am going with the good old fashioned config file. The problem is, stuff outputs twice to the console and I can't understand why. Here's my test script:
import logging
import logging.config
if __name__ == "__main__":
logging.config.fileConfig("newSlogger.conf")
slogger = logging.getLogger("sloggerMain")
slogger.debug("dbg msg")
slogger.info("herp derp dominae")
Here's my config file:
[loggers]
keys=root,sloggerMain,sloggerSecondary
[handlers]
keys=consoleHandler,infoFileHandler,debugFileHandler
[formatters]
keys=consoleFormatter,infoFileFormatter,debugFileFormatter
[logger_root]
handlers=consoleHandler
level=NOTSET
[logger_sloggerMain]
handlers=consoleHandler,infoFileHandler,debugFileHandler
level=DEBUG
qualname=sloggerMain
[logger_sloggerSecondary]
handlers=consoleHandler,infoFileHandler,debugFileHandler
level=DEBUG
qualname=sloggerSecondary
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
format=consoleFormatter
args=(sys.stdout,)
[handler_infoFileHandler]
class=FileHandler
level=INFO
formatter=infoFileFormatter
args=("testlog.log", "w")
[handler_debugFileHandler]
class=FileHandler
level=DEBUG
formatter=debugFileFormatter
args=("testlogdbg.log", "w")
[formatter_consoleFormatter]
format=%(name)s: %(asctime)s %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S
[formatter_infoFileFormatter]
format=%(name)s: %(asctime)s %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S
[formatter_debugFileFormatter]
format=%(name)s: %(asctime)s %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S
[formatter_syslogFormatter]
format=%(name)s: %(asctime)s %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S
Any ideas?
Change your non-root loggers to set
propagate
to 0, to prevent messages from being propagated up to the root logger:The
logging
module's docs say:The
sloggerMain
logger is a child of theroot
logger. By default, messages emitted to that logger are also propagated upward.You can also simply disable root logging to fix the problem: