Dear all,

I am quite confused about the Python logging. I have read and re-read the Python documentation for the Python logging module and googled, but to no avail. I simply want one logger to log to a file and another logger to log to the console. Neither should log the other's messages. The following code causes fileLogger to log to BOTH a file and the console, how come? It seems to me that a call such as "consoleHandler = llogging.StreamHandler()" affects other loggers, which I do not want.

fileHandler = logging.FileHandler('../../logs/log.txt')
fileLogger = logging.getLogger('TESTHARNESSFILE')
fileLogger.addHandler(fileHandler)
fileLogger.setLevel(logging.INFO)

consoleHandler = logging.StreamHandler()
logger = logging.getLogger('TESTHARNESS')
logger.addHandler(consoleHandler)
logger.setLevel(logging.INFO)

Thanks for any help, I have spent so long trying to figure this out!

Hi all. I think I fixed this problem by setting fileLogger.propagate = 0. Otherwise it will propagate up to the root logger, which outputs to stdout, as far as I can understand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.