Python logging external library messages to file

Viewed 110

I have a myapp.py that looks like:

import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

fh = logging.FileHandler('out.log')
fh.setLevel(logging.INFO)
logger.addHandler(fh)


import ext_library
logger.info('Starting...')
ext_library.do_something()

.do_something() outputs a bunch of INFO messages from different modules/functions of ext_library. It looks like:

[INFO 05-10 19:40:59] ext_library.model.utils: Some information here.
[INFO 05-10 19:40:59] ext_library.service: Started to run inner function.
[INFO 05-10 19:40:59] ext_library.service: Finishing...

All of ext_library has loggers following the logger = logging.getLogger(__name__) format. I would like everything to be logged to out.log. What I'm finding is that:

  • Starting... and all the INFO messages coming from ext_library are displayed on the terminal.
  • None of the INFO messages from ext_library are getting logged to out.log.

What am I doing wrong? I also find that even if I change the log level of logger to logging.ERROR, all the INFO messages from ext_library are still appearing on the terminal which is not what I expected... am I not propagating the setLevel and addHandler correctly?

Thanks.

0 Answers
Related