Repository: incubator-ariatosca Updated Branches: refs/heads/ARIA-146-Support-colorful-execution-logging ca1baab67 -> 909ccaace
stylized the logs Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/909ccaac Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/909ccaac Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/909ccaac Branch: refs/heads/ARIA-146-Support-colorful-execution-logging Commit: 909ccaacedcd8366573470e0f60c39a91b69b730 Parents: ca1baab Author: max-orlov <ma...@gigaspaces.com> Authored: Fri Apr 21 13:53:34 2017 +0300 Committer: max-orlov <ma...@gigaspaces.com> Committed: Fri Apr 21 13:53:34 2017 +0300 ---------------------------------------------------------------------- aria/cli/color.py | 41 ++++++------- aria/cli/execution_logging.py | 120 ++++++++++++++++++++++--------------- 2 files changed, 90 insertions(+), 71 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/909ccaac/aria/cli/color.py ---------------------------------------------------------------------- diff --git a/aria/cli/color.py b/aria/cli/color.py index 7b20d30..ed71238 100644 --- a/aria/cli/color.py +++ b/aria/cli/color.py @@ -16,45 +16,38 @@ from StringIO import StringIO import colorama -colorama.init() - class StyledString(object): - fore = colorama.Fore - back = colorama.Back - style = colorama.Style + FORE = colorama.Fore + BACK = colorama.Back + STYLE = colorama.Style - def __init__(self, str_to_stylize, *args, **kwargs): + def __init__(self, str_to_stylize, *style_args): """ :param str_to_stylize: - :param args: + :param style_args: :param kwargs: to_close: specifies if end the format on the current line. default to True """ - super(StyledString, self).__init__() - # TODO: raise proper exception - if not all(self._is_valid(arg) for arg in args): - raise Exception("bla bla bla") - self._str = str_to_stylize - self._args = args - self._stylized_str = None + colorama.init() + self._original_str = str_to_stylize + self._args = style_args + self._apply_style() def __str__(self): - if self._stylized_str is None: - styling_str = StringIO() - self._apply_style(styling_str) - styling_str.write(self._str) - styling_str.write(self.style.RESET_ALL) - self.stylized_str = styling_str.getvalue() + return self._stylized_str - return self.stylized_str + def _apply_style(self): + assert all(self._is_valid(arg) for arg in self._args) - def _apply_style(self, styling_str): + styling_str = StringIO() for arg in self._args: styling_str.write(arg) + styling_str.write(self._original_str) + styling_str.write(self.STYLE.RESET_ALL) + self._stylized_str = styling_str.getvalue() def _is_valid(self, value): - return any(value in vars(instance).values() - for instance in (self.fore, self.back, self.style)) + return any(value in vars(type).values() for type in (self.FORE, self.BACK, self.STYLE)) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/909ccaac/aria/cli/execution_logging.py ---------------------------------------------------------------------- diff --git a/aria/cli/execution_logging.py b/aria/cli/execution_logging.py index 0743abd..6019b94 100644 --- a/aria/cli/execution_logging.py +++ b/aria/cli/execution_logging.py @@ -14,71 +14,94 @@ # limitations under the License. import os from StringIO import StringIO +from functools import partial from .color import StyledString from . import logger from .env import env + +LEVEL = 'level' +TIMESTAMP = 'timestamp' +MESSAGE = 'message' +IMPLEMENTATION = 'implementation' +INPUTS = 'inputs' +TRACEBACK = 'traceback' + + DEFAULT_FORMATTING = { logger.NO_VERBOSE: {'message': '{message}'}, logger.LOW_VERBOSE: { - 'message': '{timestamp} | {level} | {message}', - 'level': '{level[0]}', - 'timestamp': '%H:%M:%S', + MESSAGE: '{timestamp} | {level} | {message}', + LEVEL: '{level[0]}', + TIMESTAMP: '%H:%M:%S', }, logger.MEDIUM_VERBOSE: { - 'message': '{timestamp} | {level} | {implementation} | {message} ', - 'level': '{level[0]}', - 'timestamp': '%H:%M:%S' + MESSAGE: '{timestamp} | {level} | {implementation} | {message} ', + LEVEL: '{level[0]}', + TIMESTAMP: '%H:%M:%S' }, logger.HIGH_VERBOSE: { - 'message': '{timestamp} | {level} | {implementation}({inputs}) | {message} ', - 'level': '{level[0]}', - 'timestamp': '%H:%M:%S' + MESSAGE: '{timestamp} | {level} | {implementation} | {inputs} | {message} ', + LEVEL: '{level[0]}', + TIMESTAMP: '%H:%M:%S' }, } - -def _style_level(level): - levels_format = { - 'info': (StyledString.fore.LIGHTBLUE_EX, ), - 'debug': (StyledString.fore.LIGHTRED_EX, StyledString.style.DIM), - 'error': (StyledString.fore.RED, ) +DEFAULT_STYLES = { + LEVEL: { + 'info': (StyledString.FORE.LIGHTMAGENTA_EX,), + 'debug': (StyledString.FORE.LIGHTMAGENTA_EX, StyledString.STYLE.DIM), + 'error': (StyledString.FORE.RED, StyledString.STYLE.BRIGHT) + }, + TIMESTAMP: { + 'info': (StyledString.FORE.LIGHTMAGENTA_EX,), + 'debug': (StyledString.FORE.LIGHTMAGENTA_EX, StyledString.STYLE.DIM), + 'error': (StyledString.FORE.RED, StyledString.STYLE.BRIGHT) + }, + MESSAGE: { + 'info': (StyledString.FORE.LIGHTBLUE_EX,), + 'debug': (StyledString.FORE.LIGHTBLUE_EX, StyledString.STYLE.DIM), + 'error': (StyledString.FORE.RED, StyledString.STYLE.BRIGHT), + }, + IMPLEMENTATION: { + 'info': (StyledString.FORE.LIGHTBLACK_EX,), + 'debug': (StyledString.FORE.LIGHTBLACK_EX, StyledString.STYLE.DIM,), + 'error': (StyledString.FORE.RED, StyledString.STYLE.BRIGHT,), + }, + INPUTS: { + 'info': (StyledString.FORE.BLUE,), + 'debug': (StyledString.FORE.BLUE, StyledString.STYLE.DIM), + 'error': (StyledString.FORE.RED, StyledString.STYLE.BRIGHT,), + }, + TRACEBACK: { + 'error': (StyledString.FORE.RED, ) } - return StyledString(level[0], *levels_format.get(level.lower(), [])) +} -def _style_timestamp(timestamp, level): - timestamp_format = { - 'info': (StyledString.fore.LIGHTBLUE_EX, StyledString.style.DIM), - 'debug': (StyledString.fore.LIGHTBLUE_EX, StyledString.style.DIM), - 'error': (StyledString.fore.RED,) - } - return StyledString(timestamp, *timestamp_format.get(level.lower(), [])) +class _StylizedLogs(object): + def __init__(self, styles=None): + self._styles = styles or DEFAULT_STYLES -def _style_msg(msg, level): - msg_foramts = { - 'info': (StyledString.fore.LIGHTBLUE_EX, ), - 'debug': (StyledString.fore.LIGHTBLUE_EX, StyledString.style.DIM), - 'error': (StyledString.fore.RED, ), - } - return StyledString(msg, *msg_foramts.get(level.lower(), [])) + def set_styles(self, styles): + self._styles = styles + def unset_styles(self, to_defaults=False): + self._styles = DEFAULT_STYLES if to_defaults else {} -def _style_traceback(traceback): - return StyledString(traceback, StyledString.fore.RED, StyledString.style.DIM) + def __getattr__(self, item): + return partial(self._style, style_type=item) + def level(self, level): + return self._style(level[0], level, LEVEL) + + def _style(self, msg, level, style_type): + return StyledString(msg, *self._styles[style_type].get(level.lower(), [])) -def _style_implementation(implementation, level): - implementation_formats = { - 'info': (StyledString.style.DIM, StyledString.fore.LIGHTBLACK_EX), - 'debug': (StyledString.style.DIM, StyledString.fore.LIGHTBLACK_EX), - 'error': (StyledString.fore.RED, ), - } - return StyledString(implementation, *implementation_formats.get(level.lower(), [])) -_style_inputs = _style_implementation +stylized_log = _StylizedLogs() def _str(item, formats=None): @@ -90,35 +113,38 @@ def _str(item, formats=None): formatting_kwargs = dict(item=item) # level - formatting_kwargs['level'] = _style_level(item.level) + formatting_kwargs['level'] = stylized_log.level(item.level) # implementation if item.task: + # operation task implementation = item.task.implementation inputs = dict(i.unwrap() for i in item.task.inputs.values()) else: + # execution task implementation = item.execution.workflow_name inputs = dict(i.unwrap() for i in item.execution.inputs.values()) - formatting_kwargs['implementation'] = _style_implementation(implementation, item.level) - formatting_kwargs['inputs'] = _style_inputs(inputs, item.level) + formatting_kwargs['implementation'] = stylized_log.implementation(implementation, item.level) + formatting_kwargs['inputs'] = stylized_log.inputs(inputs, item.level) # timestamp if 'timestamp' in formatting: timestamp = item.created_at.strftime(formatting['timestamp']) else: timestamp = item.created_at - formatting_kwargs['timestamp'] = _style_timestamp(timestamp, item.level) + formatting_kwargs['timestamp'] = stylized_log.timestamp(timestamp, item.level) # message - formatting_kwargs['message'] = _style_msg(item.msg, item.level) + formatting_kwargs['message'] = stylized_log.message(item.msg, item.level) - msg.write(formatting['message'].format(**formatting_kwargs) + os.linesep) + msg.write(formatting['message'].format(**formatting_kwargs)) # Add the exception and the error msg. if item.traceback and env.logging.verbosity_level >= logger.MEDIUM_VERBOSE: + msg.write(os.linesep) for line in item.traceback.splitlines(True): - msg.write(_style_traceback('\t' + '|' + line)) + msg.write(stylized_log.traceback('\t' + '|' + line, item.level)) return msg.getvalue()