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:
[logger_sloggerMain]
handlers=consoleHandler,infoFileHandler,debugFileHandler
level=DEBUG
qualname=sloggerMain
propagate=0
The logging
module's docs say:
Child loggers propagate messages up to the handlers associated with their ancestor loggers. Because of this, it is unnecessary to define and configure handlers for all the loggers an application uses. It is sufficient to configure handlers for a top-level logger and create child loggers as needed.
The sloggerMain
logger is a child of the root
logger. By default, messages emitted to that logger are also propagated upward.
You can also simply disable root logging to fix the problem:
[logger_root]
handlers=
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With