shikhanshu
shikhanshu

Reputation: 1548

Ignore logging.basicConfig in third party module

I am importing a third party module that I don't have control over and can't change the source. It does a logging.basicConfig() which is messing up my own logger configuration. I have tried giving a different 'name' to my logger using self.logger = logging.getLogger('some_random_name') which is resulting in every log message printed twice, once with my format, and once with the format set by the basicConfig in that third party module.

Is there a way to ignore the basicConfig from the imported module?

Upvotes: 1

Views: 766

Answers (1)

Sraw
Sraw

Reputation: 20214

logging.basicConfig() implicitly add handlers to the root logger, so you can just delete those handlers by logging.root.handlers = [].


Further, the root logger may set to an unwanted level, you can also simply set it by logging.root.setLevel(what_ever_you_want).


Even further, if you call logging.info, logging.error etc. without configuring the root logger first, a basicConfig() is called internally, and so a StreamHandler will be implicitly added to the root logger.

Upvotes: 3

Related Questions