[ https://issues.apache.org/jira/browse/ARIA-146?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15986350#comment-15986350 ]
ASF GitHub Bot commented on ARIA-146: ------------------------------------- Github user mxmrlv commented on a diff in the pull request: https://github.com/apache/incubator-ariatosca/pull/109#discussion_r113657022 --- Diff: aria/cli/execution_logging.py --- @@ -12,67 +12,228 @@ # 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 +import re from StringIO import StringIO from . import logger +from .color import Color from .env import env -DEFAULT_FORMATTING = { - logger.NO_VERBOSE: {'message': '{item.msg}'}, - logger.LOW_VERBOSE: { - 'message': '{timestamp} | {item.level[0]} | {item.msg}', - 'timestamp': '%H:%M:%S' + +FIELD_TYPE = 'field_type' +LEVEL = 'level' +TIMESTAMP = 'timestamp' +MESSAGE = 'message' +IMPLEMENTATION = 'implementation' +INPUTS = 'inputs' +TRACEBACK = 'traceback' +MARKER = 'marker' + +FINAL_STATES = 'final_states' +SUCCESS_STATE = 'success' +CANCEL_STATE = 'cancel' +FAIL_STATE = 'fail' + +_EXECUTION_BASE_PATTERN = "\'.*\' workflow execution " +_SUCCESSFUL_EXECUTION_PATTERN = _EXECUTION_BASE_PATTERN + "succeeded" +_FAILED_EXECUTION_PATTERN = _EXECUTION_BASE_PATTERN + "failed" +_CANCELED_EXECUTION_PATTERN = _EXECUTION_BASE_PATTERN + "canceled" + +_TIMESTAMP_PATTERN = '.*({timestamp.*?}).*' +_LEVEL_PATTERN = '.*({level.*?}).*' +_MESSAGE_PATTERN = '.*({message.*?}).*' +_IMPLEMENTATION_PATTERN = '.*({implementation.*?}).*' +_INPUTS_PATTERN = '.*({inputs.*?}).*' + +_PATTERNS = { + FINAL_STATES: { + SUCCESS_STATE: re.compile(_SUCCESSFUL_EXECUTION_PATTERN), + CANCEL_STATE: re.compile(_CANCELED_EXECUTION_PATTERN), + FAIL_STATE: re.compile(_FAILED_EXECUTION_PATTERN), }, - logger.MEDIUM_VERBOSE: { - 'message': '{timestamp} | {item.level[0]} | {implementation} | {item.msg} ', - 'timestamp': '%H:%M:%S' + FIELD_TYPE: { + IMPLEMENTATION: re.compile(_IMPLEMENTATION_PATTERN), + LEVEL: re.compile(_LEVEL_PATTERN), + MESSAGE: re.compile(_MESSAGE_PATTERN), + INPUTS: re.compile(_INPUTS_PATTERN), + TIMESTAMP: re.compile(_TIMESTAMP_PATTERN) + } +} + +_FINAL_STATES = { + SUCCESS_STATE: Color.Fore.GREEN, + CANCEL_STATE: Color.Fore.YELLOW, + FAIL_STATE: Color.Fore.RED +} + +_DEFAULT_STYLE = { + LEVEL: { + 'info': {'fore': 'lightmagenta_ex'}, + 'debug': {'fore': 'lightmagenta_ex', 'style': 'dim'}, + 'error': {'fore': 'red', 'style': 'bright'}, }, - logger.HIGH_VERBOSE: { - 'message': '{timestamp} | {item.level[0]} | {implementation}({inputs}) | {item.msg} ', - 'timestamp': '%H:%M:%S' + TIMESTAMP: { + 'info': {'fore': 'lightmagenta_ex'}, + 'debug': {'fore': 'lightmagenta_ex', 'style': 'dim'}, + 'error': {'fore': 'red', 'style': 'bright'}, }, -} + MESSAGE: { + 'info': {'fore': 'lightblue_ex'}, + 'debug': {'fore': 'lightblue_ex', 'style': 'dim'}, + 'error': {'fore': 'red', 'style': 'bright'}, + }, + IMPLEMENTATION:{ + 'info': {'fore': 'lightblack_ex'}, + 'debug': {'fore': 'lightblack_ex', 'style': 'dim'}, + 'error': {'fore': 'red', 'style': 'bright'}, + }, + INPUTS: { + 'info': {'fore': 'blue'}, + 'debug': {'fore': 'blue', 'style': 'dim'}, + 'error': {'fore': 'red', 'style': 'bright'}, + }, + TRACEBACK: {'error': {'fore': 'red'}}, + MARKER: 'lightyellow_ex' +} -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) +def stylize_log(item, mark_pattern): + # implementation if item.task: - formatting_kwargs['implementation'] = item.task.implementation - formatting_kwargs['inputs'] = dict(i.unwrap() for i in item.task.inputs.values()) + # operation task + 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()) + # execution task + implementation = item.execution.workflow_name + inputs = dict(i.unwrap() for i in item.execution.inputs.values()) - if 'timestamp' in formatting: - formatting_kwargs['timestamp'] = item.created_at.strftime(formatting['timestamp']) - else: - formatting_kwargs['timestamp'] = item.created_at - - msg.write(formatting['message'].format(**formatting_kwargs)) + # TODO: use the is_workflow_log + stylized_str = Color.stylize(_get_format()) + _update_level(stylized_str, item) + _update_timestamp(stylized_str, item.created_at, item) + _update_message(stylized_str, item.msg, item, mark_pattern) + _update_inputs(stylized_str, inputs, item, mark_pattern) + _update_implementation(stylized_str, implementation, item, mark_pattern) + msg = StringIO() + msg.write(str(stylized_str)) # 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(os.linesep) + msg.writelines(_get_traceback('\t' + '|' + line, item, mark_pattern) + for line in item.traceback.splitlines(True)) return msg.getvalue() -def log(item, *args, **kwargs): - return getattr(env.logging.logger, item.level.lower())(_str(item), *args, **kwargs) +def log(item, mark_pattern=None, *args, **kwargs): + leveled_log = getattr(env.logging.logger, item.level.lower()) + return leveled_log(stylize_log(item, mark_pattern), *args, **kwargs) -def log_list(iterator): +def log_list(iterator, mark_pattern=None): any_logs = False for item in iterator: - log(item) + log(item, mark_pattern) any_logs = True return any_logs + + +def _find_pattern(pattern, field_value): + # TODO: this finds the matching field type according to a pattern + match = re.match(pattern, field_value) + if match: + return match.group(1) + + +def _get_format(): + return env.config.logging.formats[env.logging.verbosity_level] + + +def _styles(field_type): --- End diff -- get > Support colorful execution logging > ---------------------------------- > > Key: ARIA-146 > URL: https://issues.apache.org/jira/browse/ARIA-146 > Project: AriaTosca > Issue Type: Story > Reporter: Ran Ziv > Assignee: Maxim Orlov > Priority: Minor > > Add support for printing execution logs in color -- This message was sent by Atlassian JIRA (v6.3.15#6346)