Repository: incubator-ariatosca Updated Branches: refs/heads/ARIA-146-Support-colorful-execution-logging 3a7657431 -> ca1baab67
wip2 Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/ca1baab6 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/ca1baab6 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/ca1baab6 Branch: refs/heads/ARIA-146-Support-colorful-execution-logging Commit: ca1baab678aa40057eacf5d08a5a83c89074b830 Parents: 3a76574 Author: max-orlov <[email protected]> Authored: Thu Apr 20 20:18:11 2017 +0300 Committer: max-orlov <[email protected]> Committed: Thu Apr 20 20:18:11 2017 +0300 ---------------------------------------------------------------------- aria/cli/color.py | 20 ++++++--- aria/cli/execution_logging.py | 92 +++++++++++++++++++++++++++++--------- 2 files changed, 86 insertions(+), 26 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/ca1baab6/aria/cli/color.py ---------------------------------------------------------------------- diff --git a/aria/cli/color.py b/aria/cli/color.py index 54ee4bf..7b20d30 100644 --- a/aria/cli/color.py +++ b/aria/cli/color.py @@ -19,32 +19,42 @@ import colorama colorama.init() -class StyledString(str): +class StyledString(object): fore = colorama.Fore back = colorama.Back style = colorama.Style - def __init__(self, str_, *args): + def __init__(self, str_to_stylize, *args, **kwargs): + """ + + :param str_to_stylize: + :param 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_ + self._str = str_to_stylize self._args = args self._stylized_str = None def __str__(self): if self._stylized_str is None: styling_str = StringIO() - for arg in self._args: - styling_str.write(arg) + 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 + def _apply_style(self, styling_str): + for arg in self._args: + styling_str.write(arg) + def _is_valid(self, value): return any(value in vars(instance).values() for instance in (self.fore, self.back, self.style)) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/ca1baab6/aria/cli/execution_logging.py ---------------------------------------------------------------------- diff --git a/aria/cli/execution_logging.py b/aria/cli/execution_logging.py index 17e396d..0743abd 100644 --- a/aria/cli/execution_logging.py +++ b/aria/cli/execution_logging.py @@ -12,7 +12,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +import os from StringIO import StringIO from .color import StyledString @@ -20,22 +20,67 @@ from . import logger from .env import env DEFAULT_FORMATTING = { - logger.NO_VERBOSE: {'message': '{item.msg}'}, + logger.NO_VERBOSE: {'message': '{message}'}, logger.LOW_VERBOSE: { - 'message': '{timestamp} | {level} | {item.msg}', + 'message': '{timestamp} | {level} | {message}', + 'level': '{level[0]}', 'timestamp': '%H:%M:%S', }, logger.MEDIUM_VERBOSE: { - 'message': '{timestamp} | {level} | {implementation} | {item.msg} ', + 'message': '{timestamp} | {level} | {implementation} | {message} ', + 'level': '{level[0]}', 'timestamp': '%H:%M:%S' }, logger.HIGH_VERBOSE: { - 'message': '{timestamp} | {level} | {implementation}({inputs}) | {item.msg} ', + '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, ) + } + 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(), [])) + + +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 _style_traceback(traceback): + return StyledString(traceback, StyledString.fore.RED, StyledString.style.DIM) + + +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 + + def _str(item, formats=None): # If no formats are passed we revert to the default formats (per level) formats = formats or {} @@ -44,33 +89,36 @@ def _str(item, formats=None): msg = StringIO() formatting_kwargs = dict(item=item) - # region level styling - - levels_format = { - 'debug': (StyledString.back.LIGHTRED_EX,) - } - formatting_kwargs['level'] = StyledString(item.level[0], *levels_format.get(item.level.lower(), [])) - - # endregion level styling + # level + formatting_kwargs['level'] = _style_level(item.level) + # implementation if item.task: - formatting_kwargs['implementation'] = item.task.implementation - formatting_kwargs['inputs'] = dict(i.unwrap() for i in item.task.inputs.values()) + implementation = item.task.implementation + inputs = dict(i.unwrap() for i in item.task.inputs.values()) else: - formatting_kwargs['implementation'] = item.execution.workflow_name - formatting_kwargs['inputs'] = dict(i.unwrap() for i in item.execution.inputs.values()) + 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) + # timestamp if 'timestamp' in formatting: - formatting_kwargs['timestamp'] = item.created_at.strftime(formatting['timestamp']) + timestamp = item.created_at.strftime(formatting['timestamp']) else: - formatting_kwargs['timestamp'] = item.created_at + timestamp = item.created_at + formatting_kwargs['timestamp'] = _style_timestamp(timestamp, item.level) - msg.write(formatting['message'].format(**formatting_kwargs)) + # message + formatting_kwargs['message'] = _style_msg(item.msg, item.level) + + msg.write(formatting['message'].format(**formatting_kwargs) + os.linesep) # Add the exception and the error msg. if item.traceback and env.logging.verbosity_level >= logger.MEDIUM_VERBOSE: for line in item.traceback.splitlines(True): - msg.write('\t' + '|' + line) + msg.write(_style_traceback('\t' + '|' + line)) return msg.getvalue() @@ -85,3 +133,5 @@ def log_list(iterator): log(item) any_logs = True return any_logs + +
