Exemplo n.º 1
0
    def _set_log_level(proto_level):
        """
        Set log level based on proto-enum level
        """
        if proto_level == LogLevel.Value('DEBUG'):
            level = logging.DEBUG
        elif proto_level == LogLevel.Value('INFO'):
            level = logging.INFO
        elif proto_level == LogLevel.Value('WARNING'):
            level = logging.WARNING
        elif proto_level == LogLevel.Value('ERROR'):
            level = logging.ERROR
        elif proto_level == LogLevel.Value('FATAL'):
            level = logging.FATAL
        else:
            logging.error(
                'Unknown logging level: %d, defaulting to INFO',
                proto_level,
            )
            level = logging.INFO

        logging.info(
            "Setting logging level to %s",
            logging.getLevelName(level),
        )
        logger = logging.getLogger('')
        logger.setLevel(level)
Exemplo n.º 2
0
def create_parser():
    """
    Creates the argparse parser with all the arguments.
    """
    parser = argparse.ArgumentParser(
        description='Management CLI for Service303',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    # Add subcommands
    subparsers = parser.add_subparsers(title='subcommands', dest='cmd')
    parser_metrics = subparsers.add_parser('metrics', help='Get service metrics')
    parser_stop = subparsers.add_parser('stop', help='Stop service')
    parser_info = subparsers.add_parser('info', help='Get service info')

    parser_log_level = subparsers.add_parser('log_level', help='Set log level')
    parser_log_level.add_argument(
        'level', help='Log level', choices=LogLevel.keys())

    # Add arguments
    for cmd in [parser_metrics, parser_info, parser_stop, parser_log_level]:
        cmd.add_argument('service', help='Service identifier')

    # Add function callbacks
    parser_metrics.set_defaults(func=get_metrics)
    parser_stop.set_defaults(func=stop_service)
    parser_info.set_defaults(func=get_info)
    parser_log_level.set_defaults(func=set_log_level)
    return parser
Exemplo n.º 3
0
    def _setup_logging(self):
        """
        Setup the logging for the service
        """
        if self._config is None:
            config_level = None
        else:
            config_level = self._config.get('log_level', None)

        try:
            proto_level = LogLevel.Value(config_level)
        except ValueError:
            logging.error(
                'Unknown logging level in config: %s, defaulting to INFO',
                config_level)
            proto_level = LogLevel.Value('INFO')
        self._set_log_level(proto_level)
Exemplo n.º 4
0
 def _get_log_level_from_config(self) -> Optional[int]:
     if self._config is None:
         return None
     log_level = self._config.get('log_level', None)
     if log_level is None:
         return None
     # convert from log level string to LogLevel enum value
     try:
         proto_level = LogLevel.Value(log_level)
     except ValueError:
         logging.error(
             'Unknown logging level in config: %s, ignoring',
             log_level,
         )
         return None
     return proto_level
Exemplo n.º 5
0
    def _setup_logging(self):
        """Set up log level from config values

        The config file on the AGW takes precedence over the mconfig
        If neither config file nor mconfig has the log level config, default to INFO
        """
        log_level_from_config = self._get_log_level_from_config()
        log_level_from_mconfig = self._get_log_level_from_mconfig()

        if log_level_from_config:
            log_level = log_level_from_config
        elif log_level_from_mconfig:
            log_level = log_level_from_mconfig
        else:
            logging.warning(
                'logging level is not specified in either yml config '
                'or mconfig, defaulting to INFO', )
            log_level = LogLevel.Value('INFO')
        self._set_log_level(log_level)
Exemplo n.º 6
0
def set_log_level(client, args):
    message = LogLevelMessage()
    message.level = LogLevel.Value(args.level)
    client.SetLogLevel(message)