9

Suppose your code is using a module that uses log statements with logging.info from the module as opposed to logger.info from a logger instance, and that you don't have control to modify the module that is in use.

Is it possible to customize the logging levels for JUST the imported module without requesting a code change from the maintainers?

These questions have described a very straightforward approach for changing the logging level for a particular module.
How do I disable log messages from the Requests library?
Python Logging - Disable logging from imported modules

The code below implements that solution as described to log only ERROR messages from that module, but it did not turn of WARNING messages as expected.

The test module produces the same output when referring to the noisy_noise module as a string literal and by using the name of the imported symbol.

What is missing from run_me.py to suppress WARN messages?

noisy_noise.py

EDIT: Assume this module represents one that is imported from other maintainers and is closed for modification

import logging

def log_things():
    logging.warn('WARNING')
    logging.info('INFORMATION')
    logging.error('DANGER')

run_me.py

import logging
import noisy_noise
import sys

def main(args):
    logging.getLogger(noisy_noise.__name__).setLevel(logging.ERROR)
#    logging.getLogger('noisy_noise').setLevel(logging.ERROR)

    noisy_noise.log_things()

if __name__ == '__main__':
   sys.exit(main(sys.argv[1:]))
$ python run_me.py
WARNING:root:WARNING
ERROR:root:DANGER
codeforester
  • 39,467
  • 16
  • 112
  • 140
Val H
  • 507
  • 4
  • 13
  • You should use `logger.warn` etc. instead of `logging.warn` for the specific logger you are trying to work with – Zionsof Jun 26 '19 at 15:38
  • Hi @Zionsof, thanks for the tip. I see that level modifications work as expected when the target module uses logger instead of logging. In this case, noisy_noise.py is standing in to represent a module that I'm importing but not editing. Are there any options to suppress a module that from the developer's POV is closed to modification? I'll update the question to reflect this – Val H Jun 26 '19 at 18:50
  • If the module is using the default logger, and you have no control over it, then I don't think there is a way to do a specific one unless you know its name. If you wish to disable all existing loggers, you can use something like `logging.config.dictConfig({ 'version': 1, 'disable_existing_loggers': True, })`. Also maybe have a look at [this](https://stackoverflow.com/questions/11029717/how-do-i-disable-log-messages-from-the-requests-library) – Zionsof Jun 27 '19 at 04:56

1 Answers1

8

Yes it is possible if you know the name of the logger.

Example: change the log level of urllib3 to WARNING

logging.getLogger("urllib3").setLevel(logging.WARNING)
balderman
  • 22,927
  • 7
  • 34
  • 52
  • Thanks for your answer @balderman. In this case, the module doing the logging is using the default logger. – Val H Jun 26 '19 at 19:38
  • 1
    In that case I think you will not be able to control the log level. If you have control of the other module code you better use a named logger. – balderman Jun 26 '19 at 19:39
  • 1
    Example module `urllib3` is chosen carefully :) – flaschbier Oct 11 '21 at 12:25