added markingup
Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/1fc9ac75 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/1fc9ac75 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/1fc9ac75 Branch: refs/heads/ARIA-146-Support-colorful-execution-logging Commit: 1fc9ac756b39db9d14acc3cd47efcc8549a16039 Parents: dbcef5f Author: max-orlov <[email protected]> Authored: Fri Apr 21 19:48:07 2017 +0300 Committer: max-orlov <[email protected]> Committed: Fri Apr 21 20:49:08 2017 +0300 ---------------------------------------------------------------------- aria/cli/commands/logs.py | 5 +- aria/cli/core/aria.py | 9 +++ aria/cli/execution_logging.py | 129 +++++++++++++++++++++---------------- 3 files changed, 85 insertions(+), 58 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/1fc9ac75/aria/cli/commands/logs.py ---------------------------------------------------------------------- diff --git a/aria/cli/commands/logs.py b/aria/cli/commands/logs.py index 79aff07..f52cfc1 100644 --- a/aria/cli/commands/logs.py +++ b/aria/cli/commands/logs.py @@ -28,15 +28,18 @@ def logs(): short_help='List execution logs') @aria.argument('execution-id') @aria.options.verbose() [email protected]_pattern() @aria.pass_model_storage @aria.pass_logger -def list(execution_id, model_storage, logger): +def list(execution_id, mark_pattern, model_storage, logger): """Display logs for an execution """ logger.info('Listing logs for execution id {0}'.format(execution_id)) log_iterator = ModelLogIterator(model_storage, execution_id) + execution_logging.stylized_log.set(mark_pattern=mark_pattern) any_logs = execution_logging.log_list(log_iterator) + execution_logging.stylized_log.reset(to_defaults=True) if not any_logs: logger.info('\tNo logs') http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/1fc9ac75/aria/cli/core/aria.py ---------------------------------------------------------------------- diff --git a/aria/cli/core/aria.py b/aria/cli/core/aria.py index ed7c490..6436c3e 100644 --- a/aria/cli/core/aria.py +++ b/aria/cli/core/aria.py @@ -425,5 +425,14 @@ class Options(object): required=required, help=helptexts.SERVICE_ID) + @staticmethod + def mark_pattern(default=None): + return click.option( + '-m', + '--mark-pattern', + help='pattern this', + type=str, + default=default + ) options = Options() http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/1fc9ac75/aria/cli/execution_logging.py ---------------------------------------------------------------------- diff --git a/aria/cli/execution_logging.py b/aria/cli/execution_logging.py index f8eec22..17ea774 100644 --- a/aria/cli/execution_logging.py +++ b/aria/cli/execution_logging.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import os +import re from StringIO import StringIO from functools import partial @@ -27,7 +28,7 @@ MESSAGE = 'message' IMPLEMENTATION = 'implementation' INPUTS = 'inputs' TRACEBACK = 'traceback' - +MARKER = 'marker' DEFAULT_FORMATTING = { logger.NO_VERBOSE: {'message': '{message}'}, @@ -48,7 +49,7 @@ DEFAULT_FORMATTING = { }, } -DEFAULT_STYLES = { +DEFAULT_STYLING = { LEVEL: { 'info': (StyledString.FORE.LIGHTMAGENTA_EX,), 'debug': (StyledString.FORE.LIGHTMAGENTA_EX, StyledString.STYLE.DIM), @@ -76,90 +77,104 @@ DEFAULT_STYLES = { }, TRACEBACK: { 'error': (StyledString.FORE.RED, ) - } + }, + + MARKER: StyledString.BACK.LIGHTYELLOW_EX } class _StylizedLogs(object): - def __init__(self, styles=None): - self._styles = styles or DEFAULT_STYLES + def __init__(self, formats=None, styles=None): + self._formats = formats or DEFAULT_FORMATTING + self._styles = styles or DEFAULT_STYLING + self._mark_pattern = None - def set_styles(self, styles): - self._styles = styles + def set(self, styles=None, formats=None, mark_pattern=None): + self._styles = styles or DEFAULT_STYLING + self._formats = formats or DEFAULT_FORMATTING + self._mark_pattern = mark_pattern - def unset_styles(self, to_defaults=False): - self._styles = DEFAULT_STYLES if to_defaults else {} + def reset(self, to_defaults=False): + self._styles = DEFAULT_STYLING if to_defaults else {} + self._formats = DEFAULT_FORMATTING if to_defaults else {} + self._mark_pattern = None def __getattr__(self, item): - return partial(self._style, style_type=item) + return partial(self._stilize, style_type=item[1:]) - def level(self, level): - return self._style(level[0], level, LEVEL) + def _level(self, level): + return self._stilize(level[0], level, LEVEL) - def _style(self, msg, level, style_type): + def _stilize(self, msg, level, style_type): return StyledString(msg, *self._styles[style_type].get(level.lower(), [])) - def back(self, str_): - if 'web_app' not in str_: + def _mark(self, str_): + # TODO; this needs more work. since colors cause the patten not to match (since its not a continuous string) + if self._mark_pattern is None: return str_ - modified_str = StyledString.BACK.LIGHTYELLOW_EX +\ - str_.replace(StyledString.STYLE.RESET_ALL, - StyledString.STYLE.RESET_ALL + StyledString.BACK.LIGHTYELLOW_EX) + StyledString.STYLE.RESET_ALL + else: + regex_pattern = re.compile(self._mark_pattern) + if not re.match(regex_pattern, str_): + return str_ + marker = self._styles[MARKER] + modified_str = ( + marker + + str_.replace(StyledString.STYLE.RESET_ALL, StyledString.STYLE.RESET_ALL + marker) + + StyledString.STYLE.RESET_ALL + ) return modified_str -stylized_log = _StylizedLogs() + def __call__(self, item): + # If no formats are passed we revert to the default formats (per level) + formatting = self._formats.get(env.logging.verbosity_level, + DEFAULT_FORMATTING[env.logging.verbosity_level]) + msg = StringIO() + formatting_kwargs = dict(item=item) + # level + formatting_kwargs['level'] = self._level(item.level) -def _str(item, formats=None): - # If no formats are passed we revert to the default formats (per level) - formats = formats or {} - formatting = formats.get(env.logging.verbosity_level, - DEFAULT_FORMATTING[env.logging.verbosity_level]) - msg = StringIO() - formatting_kwargs = dict(item=item) + # 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()) - # level - formatting_kwargs['level'] = stylized_log.level(item.level) + formatting_kwargs['implementation'] = self._implementation(implementation, item.level) + formatting_kwargs['inputs'] = self._inputs(inputs, 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()) + # timestamp + if 'timestamp' in formatting: + timestamp = item.created_at.strftime(formatting['timestamp']) + else: + timestamp = item.created_at + formatting_kwargs['timestamp'] = self._timestamp(timestamp, item.level) - formatting_kwargs['implementation'] = stylized_log.implementation(implementation, item.level) - formatting_kwargs['inputs'] = stylized_log.inputs(inputs, item.level) + # message + formatting_kwargs['message'] = self._message(item.msg, item.level) - # timestamp - if 'timestamp' in formatting: - timestamp = item.created_at.strftime(formatting['timestamp']) - else: - timestamp = item.created_at - formatting_kwargs['timestamp'] = stylized_log.timestamp(timestamp, item.level) + # The message would be marked out if containing the provided pattern + msg.write(self._mark(formatting['message'].format(**formatting_kwargs))) - # message - formatting_kwargs['message'] = stylized_log.message(item.msg, item.level) + # 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(self._traceback('\t' + '|' + line, item.level)) - message = formatting['message'].format(**formatting_kwargs) - message = stylized_log.back(message) - msg.write(message) + return msg.getvalue() - # 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(stylized_log.traceback('\t' + '|' + line, item.level)) - return msg.getvalue() +stylized_log = _StylizedLogs() def log(item, *args, **kwargs): - return getattr(env.logging.logger, item.level.lower())(_str(item), *args, **kwargs) + return getattr(env.logging.logger, item.level.lower())(stylized_log(item), *args, **kwargs) def log_list(iterator):
