Suppress a logger by name

Viewed 45

I have some modules each implementing a class where each class creates its own logger with

logger = logging.getLogger(__name__)

and the module uses this with self.logger.info(), self.logger.debug() and so on.

Now I want to suppress the log messages from one specific class. But the class invokes the logger functions. The Python logging system allows to set the log level with

logging.basicConfig(filename='example.log',level=logging.DEBUG)

But that does not allow to specify the logger name. Would it be possible to create a dummy logger and use in the class to suppress? Are there other means to control the log output by the logger name?

1 Answers

You can get logger of third party library and set different (less verbose) level on it:

logger = logging.getLogger('third.party.library')
logger.setLevel(logging.INFO)

Since __name__ expands to fully qualified module name and loggers are hierarchical (see here) you need only set level on getLogger('third') to cover all child modules under third.

Related