Confused About Python Loggin

J

Jannik Sundø

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 code below causes fileLogger to log to BOTH a
file and the console, how come? It seems to me that a call such as
"consoleHandler = logging.StreamHandler()" affects other loggers,
which I do not want. Also, if I do not do
logging.basicConfig(level=logging.INFO) in one of the classes of my
application, the logging does not work. Any ideas how come?

Thanks a lot for any help! :)

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

Peter Otten

Jannik said:
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 code below causes fileLogger to log to BOTH a
file and the console, how come? It seems to me that a call such as
"consoleHandler = logging.StreamHandler()" affects other loggers,
which I do not want. Also, if I do not do
logging.basicConfig(level=logging.INFO) in one of the classes of my
application, the logging does not work. Any ideas how come?

Thanks a lot for any help! :)

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)

The code you present should work as you expect:

$ cat tmp.py
import logging

fileHandler = logging.FileHandler('tmp.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)


logger.info("to console")
fileLogger.info("to file")
$ python tmp.py
to console
$ cat tmp.txt
to file

What problems exactly are you running into if you omit the basicConfig()
call?

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top