Projet

Général

Profil

0001-misc-disable-logging-in-runscript-and-shell-when-in-.patch

Benjamin Dauvergne, 15 octobre 2020 17:24

Télécharger (3,11 ko)

Voir les différences:

Subject: [PATCH] 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).
 .../management/commands/__init__.py           | 21 +++++++++++++++++++
 .../management/commands/runscript.py          |  5 ++++-
 hobo/multitenant/management/commands/shell.py |  6 +++++-
 3 files changed, 30 insertions(+), 2 deletions(-)
hobo/multitenant/management/commands/__init__.py
173 173
    def _notice(self, output):
174 174
        if int(self.options.get('verbosity', 1)) >= 1:
175 175
            self.stdout.write(self.style.NOTICE(output))
176

  
177

  
178
def disable_global_logging():
179
    import os
180
    import logging
181
    import sys
182

  
183
    if 'TERM' not in os.environ:
184
        return
185

  
186
    # try to disable sentry
187
    if 'sentry_sdk' in sys.modules:
188
        import sentry_sdk
189
        sentry_sdk.init()
190

  
191
    # then try to disable journald, syslog logging and mail admins
192
    root_logger = logging.getLogger()
193
    for handler in list(root_logger.handlers):
194
        hdlr_class_name = handler.__class__.__name__
195
        if hdlr_class_name in ['JournalHandler', 'SysLogHandler', 'AdminEmailHandler']:
196
            root_logger.handlers.remove(handler)
hobo/multitenant/management/commands/runscript.py
21 21

  
22 22
from django.core.management.base import BaseCommand
23 23

  
24
class Command(BaseCommand):
24
from . import disable_logging
25

  
25 26

  
27
class Command(BaseCommand):
26 28
    def add_arguments(self, parser):
27 29
        parser.add_argument('args', nargs=argparse.REMAINDER)
28 30

  
29 31
    def handle(self, *args, **options):
32
        disable_global_logging()
30 33
        fullpath = os.path.dirname(os.path.abspath(args[0]))
31 34
        sys.path.insert(0, fullpath)
32 35
        module_name = os.path.splitext(os.path.basename(args[0]))[0]
hobo/multitenant/management/commands/shell.py
1
from hobo.multitenant.management.commands import TenantWrappedCommand
1
from hobo.multitenant.management.commands import TenantWrappedCommand, disable_global_logging
2 2
from django.core.management.commands import shell
3 3

  
4 4

  
5 5
class Command(TenantWrappedCommand):
6 6
    COMMAND = shell.Command
7

  
8
    def handle(self, *args, **kwargs):
9
        disable_global_logging()
10
        super(Command, self).handle(*args, **kwargs)
7
-