I have a use case in which we occasionally want to disable logging to Sentry via a command-line flag for debugging purposes. When we were using raven
this code did the job:
if self._parsed_args.debug:
for handler in self.logger.root.handlers:
if handler.name == "console":
# Activate console logging handler at the debug level
handler.setLevel(logging.DEBUG)
else:
# Deactivate other logging handlers by setting their
# levels very high
try:
# Assumes that the console logging level in the
# config is set to a value >logging.DEBUG
console_level = logging_config["handlers"]["console"]["level"]
except (KeyError, TypeError):
console_level = 100
handler.setLevel(console_level)
Since moving to sentry-sdk
, this no longer works in that exceptions from the script running in debug mode are logged to Sentry. I guess that means that the integrations.logging.EventHandler
instance is not included in the self.logger.root.handlers
collection?
What is the correct way to temporarily disable the Sentry handler?
I suppose I could do:
if not self._parsed_args.debug:
sentry.init()
but I am hoping for something a bit less like a sledgehammer than that