misc: disable logging in runscript and shell when in command shell (#47708)

Command shell context is guessed by the presence of the TERM environment
variable (should not happen when launched by crond, initd or systemd).
This commit is contained in:
Benjamin Dauvergne 2020-10-15 10:30:20 +02:00
parent 49691aee10
commit 59d13b7300
3 changed files with 30 additions and 2 deletions

View File

@ -173,3 +173,24 @@ class SyncCommon(BaseCommand):
def _notice(self, output):
if int(self.options.get('verbosity', 1)) >= 1:
self.stdout.write(self.style.NOTICE(output))
def disable_global_logging():
import os
import logging
import sys
if 'TERM' not in os.environ:
return
# try to disable sentry
if 'sentry_sdk' in sys.modules:
import sentry_sdk
sentry_sdk.init()
# then try to disable journald, syslog logging and mail admins
root_logger = logging.getLogger()
for handler in list(root_logger.handlers):
hdlr_class_name = handler.__class__.__name__
if hdlr_class_name in ['JournalHandler', 'SysLogHandler', 'AdminEmailHandler']:
root_logger.handlers.remove(handler)

View File

@ -21,12 +21,15 @@ import sys
from django.core.management.base import BaseCommand
class Command(BaseCommand):
from . import disable_logging
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('args', nargs=argparse.REMAINDER)
def handle(self, *args, **options):
disable_global_logging()
fullpath = os.path.dirname(os.path.abspath(args[0]))
sys.path.insert(0, fullpath)
module_name = os.path.splitext(os.path.basename(args[0]))[0]

View File

@ -1,6 +1,10 @@
from hobo.multitenant.management.commands import TenantWrappedCommand
from hobo.multitenant.management.commands import TenantWrappedCommand, disable_global_logging
from django.core.management.commands import shell
class Command(TenantWrappedCommand):
COMMAND = shell.Command
def handle(self, *args, **kwargs):
disable_global_logging()
super(Command, self).handle(*args, **kwargs)