Changeset View
Changeset View
Standalone View
Standalone View
swh/core/cli/__init__.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import logging | import logging | ||||
import logging.config | import logging.config | ||||
import signal | import signal | ||||
import click | import click | ||||
import pkg_resources | import pkg_resources | ||||
import sentry_sdk | |||||
import yaml | import yaml | ||||
LOG_LEVEL_NAMES = ['NOTSET', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'] | LOG_LEVEL_NAMES = ['NOTSET', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'] | ||||
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) | CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) | ||||
logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||
Show All 40 Lines | |||||
''' | ''' | ||||
) | ) | ||||
@click.option('--log-level', '-l', default=None, | @click.option('--log-level', '-l', default=None, | ||||
type=click.Choice(LOG_LEVEL_NAMES), | type=click.Choice(LOG_LEVEL_NAMES), | ||||
help="Log level (defaults to INFO).") | help="Log level (defaults to INFO).") | ||||
@click.option('--log-config', default=None, | @click.option('--log-config', default=None, | ||||
type=click.File('r'), | type=click.File('r'), | ||||
help="Python yaml logging configuration file.") | help="Python yaml logging configuration file.") | ||||
@click.option('--sentry-dsn', default=None, | |||||
help="DSN of the Sentry instance to report to") | |||||
@click.option('--sentry-debug/--no-sentry-debug', | |||||
default=False, hidden=True, | |||||
help="Enable debugging of sentry") | |||||
@click.pass_context | @click.pass_context | ||||
def swh(ctx, log_level, log_config): | def swh(ctx, log_level, log_config, sentry_dsn, sentry_debug): | ||||
"""Command line interface for Software Heritage. | """Command line interface for Software Heritage. | ||||
""" | """ | ||||
signal.signal(signal.SIGTERM, clean_exit_on_signal) | signal.signal(signal.SIGTERM, clean_exit_on_signal) | ||||
signal.signal(signal.SIGINT, clean_exit_on_signal) | signal.signal(signal.SIGINT, clean_exit_on_signal) | ||||
if sentry_dsn: | |||||
sentry_sdk.init(dsn=sentry_dsn, debug=sentry_debug) | |||||
if log_level is None and log_config is None: | if log_level is None and log_config is None: | ||||
log_level = 'INFO' | log_level = 'INFO' | ||||
if log_config: | if log_config: | ||||
logging.config.dictConfig(yaml.safe_load(log_config.read())) | logging.config.dictConfig(yaml.safe_load(log_config.read())) | ||||
if log_level: | if log_level: | ||||
log_level = logging.getLevelName(log_level) | log_level = logging.getLevelName(log_level) | ||||
Show All 24 Lines |