Rex
Rex

Reputation: 71

Python - How to disable logging from a module imported from the parent directory?

I have a file structure like this:

util/
└── utilA.py
src/
└── sublevel
    └── moduleB.py

In moduleB I use from util.utilA import * to import the functions from the util.

Inside moduleB, I want to disable the logging.info()'s in the util, but the following two lines wouldn't work and if I run moduleB I still see the logging generated from the functions in utilA:

logging.getLogger('util.utilA').propagate = False
logging.getLogger('util.utilA').setLevel(logging.ERROR)

I also tried logging.getLogger('util') and logging.getLogger('utilA') and neither of these worked.

One thing that also confuses me is that I used the same two lines (logging.getLogger('pdfminer')...) for another module pdfminer, and it successfully disabled the logging for that module. It just wouldn't work for my local utilA.

Can anyone help with this? Thank you!

Upvotes: 0

Views: 1002

Answers (1)

tdelaney
tdelaney

Reputation: 77337

Logging is based on the name you use when you get a logger, not the name of the module where the logging is performed. In particular, logging.info() uses the root logger. If you want to adjust logging of a given module, make sure that it is not using the root logger and use its getLogger("somename") logger name.

Upvotes: 2

Related Questions