Reputation: 110562
I have logging
set up at the INFO
level. However, I want to remove all logs from a particular module, regardless of level:
[:pid:8229 2021-06-14 19:32:06,827] - INFO paramiko.transport:_log:L1819: Connected (version 2.0, client OpenSSH_7.6p1)
[:pid:8229 2021-06-14 19:32:06,846] - INFO paramiko.transport:_log:L1819: Authentication (publickey) successful!
How would I do the equivalent of the following?
logger = logging.getLogger(self.script_name)
logger.setLevel(logging.INFO)
logger.disableLogger(paramiko) # pseudocode
I suppose one option is doing something like:
logging.getLogger("paramiko").setLevel(logging.ERROR)
Upvotes: 2
Views: 4457
Reputation: 1528
The logging
module has no concept of module-level loggers, as every logger is just established in a hierarchy based on name. paramiko.transport
will always exist as a child logger of the paramiko
logger by name, not by any module association. Because of this, there isn't really a good way to disable logging by module itself, but you can effectively disable logging for that logger and all child loggers by doing something like logging.getLogger('paramiko').setLevel(logging.CRITICAL+1)
, since every log message should propagate with a logging level that will be lower than this value, it will cause any log message to drop out before reaching a handler attached to the root logger.
Another question was raised about disabling all logging and then selectively enabling logging where needed. To achieve something like this, the quickest method would be to exclude the addition of any handlers to the root logger, and then add handlers in a "bottom-up" fashion. The reason you would want to do this is because if you end up attaching a handler to a logger that is the child of a logger that the handler has already been attached to, you will get duplicate logging messages, which is definitely of note.
Upvotes: 3