diff --git a/src/azure/cli/_argparse.py b/src/azure/cli/_argparse.py index eea8f9d3e06..5e0e7f9c3b9 100644 --- a/src/azure/cli/_argparse.py +++ b/src/azure/cli/_argparse.py @@ -4,7 +4,7 @@ import sys from ._locale import get_file as locale_get_file -from ._logging import logging +from ._logging import logger # Named arguments are prefixed with one of these strings ARG_PREFIXES = sorted(('-', '--', '/'), key=len, reverse=True) @@ -39,7 +39,7 @@ def __getattr__(self, key): return self[key] except LookupError: pass - logging.debug('Argument %s is required', key) + logger.debug('Argument %s is required', key) raise IncorrectUsageError(_("Argument {0} is required").format(key)) def _read_arg(string): @@ -165,7 +165,7 @@ def not_global(a): expected_kwargs = m['$kwargs'] handler = m['$handler'] except LookupError: - logging.debug('Missing data for noun %s', n) + logger.debug('Missing data for noun %s', n) show_usage = True if show_completions: @@ -244,7 +244,7 @@ def _display_usage(self, nouns, noun_map, arguments, out=sys.stdout): # TODO: Behave better when no docs available print('No documentation available', file=out) out.flush() - logging.debug('Expected documentation at %s', doc_file) + logger.debug('Expected documentation at %s', doc_file) def _display_completions(self, nouns, noun_map, arguments, out=sys.stdout): completions = [k for k in noun_map if not k.startswith('$')] diff --git a/src/azure/cli/_logging.py b/src/azure/cli/_logging.py index a3612cd7889..ada07b71ef4 100644 --- a/src/azure/cli/_logging.py +++ b/src/azure/cli/_logging.py @@ -3,27 +3,7 @@ __all__ = ['logging', 'configure_logging'] -_CODE_LEVEL = _logging.INFO + 1 - -class Logger(_logging.Logger): - def __init__(self, name, level = _logging.NOTSET): - super(Logger, self).__init__(name, level) - - def code(self, msg, *args): - self._log(_CODE_LEVEL, msg, args) - -logging = Logger('az', _logging.WARNING) - -class PyFileFormatter(_logging.Formatter): - def __init__(self): - super(PyFileFormatter, self).__init__('# %(levelname)s: %(message)s') - self.info_style = _logging.PercentStyle('%(message)s') - - def format(self, record): - assert isinstance(record, _logging.LogRecord) - if record.levelno == _CODE_LEVEL: - return record.getMessage() - return super(PyFileFormatter, self).format(record) +logger = _logging.Logger('az', _logging.WARNING) def _arg_name(arg): a = arg.lstrip('-/') @@ -65,18 +45,12 @@ def configure_logging(argv, config): # Configure the console output handler stderr_handler = _logging.StreamHandler(sys.stderr) stderr_handler.formatter = _logging.Formatter('%(levelname)s: %(message)s') - logging.level = stderr_handler.level = level - logging.handlers.append(stderr_handler) + logger.level = stderr_handler.level = level + logger.handlers.append(stderr_handler) - if logfile and logfile.lower().endswith('.py'): - # Configure a handler that logs code to a Python script - py_handler = _logging.StreamHandler(open(logfile, 'w', encoding='utf-8')) - py_handler.formatter = PyFileFormatter() - py_handler.level = level if level == _logging.DEBUG else _logging.INFO - logging.handlers.append(py_handler) - elif logfile: + if logfile: # Configure the handler that logs code to a text file log_handler = _logging.StreamHandler(open(logfile, 'w', encoding='utf-8')) log_handler.formatter = _logging.Formatter('[%(levelname)s:%(asctime)s] %(message)s') log_handler.level = level if level == _logging.DEBUG else _logging.INFO - logging.handlers.append(log_handler) + logger.handlers.append(log_handler) diff --git a/src/azure/cli/commands/__init__.py b/src/azure/cli/commands/__init__.py index 0f92fab95c3..598ab429816 100644 --- a/src/azure/cli/commands/__init__.py +++ b/src/azure/cli/commands/__init__.py @@ -1,5 +1,5 @@ from .._argparse import IncorrectUsageError -from .._logging import logging +from .._logging import logger # TODO: Alternatively, simply scan the directory for all modules COMMAND_MODULES = [ @@ -12,21 +12,21 @@ def command(name): def add_command(handler): _COMMANDS.setdefault(handler, {})['name'] = name - logging.debug('Added %s as command "%s"', handler, name) + logger.debug('Added %s as command "%s"', handler, name) return handler return add_command def description(description): def add_description(handler): _COMMANDS.setdefault(handler, {})['description'] = description - logging.debug('Added description "%s" to %s', description, handler) + logger.debug('Added description "%s" to %s', description, handler) return handler return add_description def option(spec, description=None): def add_option(handler): _COMMANDS.setdefault(handler, {}).setdefault('args', []).append((spec, description)) - logging.debug('Added option "%s" to %s', spec, handler) + logger.debug('Added option "%s" to %s', spec, handler) return handler return add_option diff --git a/src/azure/cli/commands/login.py b/src/azure/cli/commands/login.py index b84fb4fb846..f91a60bd323 100644 --- a/src/azure/cli/commands/login.py +++ b/src/azure/cli/commands/login.py @@ -2,7 +2,7 @@ SubscriptionClientConfiguration from msrestazure.azure_active_directory import UserPassCredentials -from .._logging import logging +from .._logging import logger from .._profile import Profile from .._util import TableOutput from ..commands import command, description, option diff --git a/src/azure/cli/commands/storage.py b/src/azure/cli/commands/storage.py index f5068fd97ca..c8d4f353bc5 100644 --- a/src/azure/cli/commands/storage.py +++ b/src/azure/cli/commands/storage.py @@ -1,5 +1,5 @@ from ..main import CONFIG, SESSION -from .._logging import logging +from .._logging import logger from .._util import TableOutput from ..commands import command, description, option from .._profile import Profile @@ -21,10 +21,8 @@ def list_accounts(args, unexpected): group = args.get('resource-group') if group: - logging.code('accounts = smc.storage_accounts.list_by_resource_group(%r)', group) accounts = smc.storage_accounts.list_by_resource_group(group) else: - logging.code('accounts = smc.storage_accounts.list()') accounts = smc.storage_accounts.list() with TableOutput() as to: @@ -42,11 +40,7 @@ def list_accounts(args, unexpected): def checkname(args, unexpected): from azure.mgmt.storage import StorageManagementClient, StorageManagementClientConfiguration - logging.code('''smc = StorageManagementClient(StorageManagementClientConfiguration()) -smc.storage_accounts.check_name_availability({0.account_name!r}) -'''.format(args)) - smc = StorageManagementClient(StorageManagementClientConfiguration()) - logging.warn(smc.storage_accounts.check_name_availability(args.account_name)) + logger.warn(smc.storage_accounts.check_name_availability(args.account_name)) diff --git a/src/azure/cli/main.py b/src/azure/cli/main.py index a090c8af4ae..3598cb5538b 100644 --- a/src/azure/cli/main.py +++ b/src/azure/cli/main.py @@ -2,7 +2,7 @@ from ._argparse import ArgumentParser from ._locale import install as locale_install -from ._logging import configure_logging, logging +from ._logging import configure_logging, logger from ._session import Session # CONFIG provides external configuration options @@ -39,7 +39,7 @@ def main(args): try: parser.execute(args) except RuntimeError as ex: - logging.error(ex.args[0]) + logger.error(ex.args[0]) return ex.args[1] if len(ex.args) >= 2 else -1 except KeyboardInterrupt: return -1 diff --git a/src/azure/cli/tests/test_argparse.py b/src/azure/cli/tests/test_argparse.py index 119158e3ccd..76491e2e435 100644 --- a/src/azure/cli/tests/test_argparse.py +++ b/src/azure/cli/tests/test_argparse.py @@ -1,7 +1,9 @@ import unittest from azure.cli._argparse import ArgumentParser, IncorrectUsageError -from azure.cli._logging import logging +from azure.cli._logging import logger +import logging + class Test_argparse(unittest.TestCase): @classmethod