You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ariatosca.apache.org by mx...@apache.org on 2017/04/12 14:19:54 UTC

[3/3] incubator-ariatosca git commit: added logs logging handlers

added logs logging handlers


Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/3cd7111e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/3cd7111e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/3cd7111e

Branch: refs/heads/ARIA-138-Make-logging-more-informative
Commit: 3cd7111e2a5a6bdefc0e648f64c24f15089b9a9e
Parents: 8e7b12c
Author: max-orlov <ma...@gigaspaces.com>
Authored: Wed Apr 12 17:18:02 2017 +0300
Committer: max-orlov <ma...@gigaspaces.com>
Committed: Wed Apr 12 17:18:02 2017 +0300

----------------------------------------------------------------------
 aria/cli/cli/aria.py               | 10 +++--
 aria/cli/commands/custom_logger.py | 57 ++++++++++++++++++++++++
 aria/cli/commands/executions.py    |  8 ++--
 aria/cli/commands/logs.py          | 12 +++--
 aria/cli/logger.py                 | 79 ++++++++++-----------------------
 5 files changed, 98 insertions(+), 68 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3cd7111e/aria/cli/cli/aria.py
----------------------------------------------------------------------
diff --git a/aria/cli/cli/aria.py b/aria/cli/cli/aria.py
index 0177134..7d94ed7 100644
--- a/aria/cli/cli/aria.py
+++ b/aria/cli/cli/aria.py
@@ -18,7 +18,7 @@ import sys
 import difflib
 import StringIO
 import traceback
-from functools import wraps
+from functools import wraps, partial
 
 import click
 
@@ -150,13 +150,17 @@ def set_cli_except_hook():
     sys.excepthook = new_excepthook
 
 
-def pass_logger(func):
+def pass_logger(func=None, log_handler=None):
     """Simply passes the logger to a command.
     """
     # Wraps here makes sure the original docstring propagates to click
+    if func is None:
+        return partial(pass_logger, log_handler=log_handler)
+
     @wraps(func)
     def wrapper(*args, **kwargs):
-        return func(logger=logger, *args, **kwargs)
+        with logger.custom_log(log_handler) as custom_logger:
+            return func(logger=custom_logger, *args, **kwargs)
 
     return wrapper
 

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3cd7111e/aria/cli/commands/custom_logger.py
----------------------------------------------------------------------
diff --git a/aria/cli/commands/custom_logger.py b/aria/cli/commands/custom_logger.py
new file mode 100644
index 0000000..1ad0fb0
--- /dev/null
+++ b/aria/cli/commands/custom_logger.py
@@ -0,0 +1,57 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# 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
+
+
+def model_logs_handler(logging, item):
+    from aria.cli.logger import NO_VERBOSE, LOW_VERBOSE, MEDIUM_VERBOSE
+
+    formats = {
+        NO_VERBOSE: {'main_msg': '{item.msg}'},
+        LOW_VERBOSE: {
+            'main_msg': '{created_at} | {item.level[0]} | {item.msg}',
+            'created_at': '%H:%M:%S'
+        }
+    }
+
+    # Only NO_VERBOSE and LOW_VERBOSE are configurable formats. configuring
+    # the low verbose level should affect any higher level.
+    formats = formats[min(logging.verbosity_level, LOW_VERBOSE)]
+
+    kwargs = dict(item=item)
+    if 'created_at' in formats:
+        kwargs['created_at'] = item.created_at.strftime(formats['created_at'])
+    if 'level' in formats:
+        kwargs['level'] = formats['level'].format(item.level)
+    if 'msg' in formats:
+        kwargs['msg'] = formats['msg'].format(item.msg)
+
+    if 'actor' in formats and item.task:
+        kwargs['actor'] = formats['actor'].format(item.task.actor)
+    if 'execution' in formats:
+        kwargs['execution'] = formats['execution'].format(item.execution)
+
+    # If no format was supplied just print out the original msg.
+    msg = formats.get('main_msg', '{item.msg}').format(**kwargs)
+
+    # Add the exception and the error msg.
+    if item.traceback and logging.verbosity_level >= MEDIUM_VERBOSE:
+        msg += os.linesep + '------>'
+        for line in item.traceback.splitlines(True):
+            msg += '\t' + '|' + line
+
+    return getattr(logging.logger, item.level.lower())(msg)
+

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3cd7111e/aria/cli/commands/executions.py
----------------------------------------------------------------------
diff --git a/aria/cli/commands/executions.py b/aria/cli/commands/executions.py
index a84a8a2..d5adab4 100644
--- a/aria/cli/commands/executions.py
+++ b/aria/cli/commands/executions.py
@@ -12,15 +12,17 @@
 # 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.
-from aria.cli.logger import LogConsumer
+
 from .. import utils
 from ..table import print_data
 from ..cli import aria
 from ...modeling.models import Execution
 from ...orchestrator.workflow_runner import WorkflowRunner
 from ...orchestrator.workflows.executor.dry import DryExecutor
+from ..logger import ModelLogConsumer
 from ...utils import formatting
 from ...utils import threading
+from . import custom_logger
 
 EXECUTION_COLUMNS = ['id', 'workflow_name', 'status', 'service_name',
                      'created_at', 'error']
@@ -109,7 +111,7 @@ def list(service_name,
 @aria.pass_model_storage
 @aria.pass_resource_storage
 @aria.pass_plugin_manager
-@aria.pass_logger
+@aria.pass_logger(log_handler=custom_logger.model_logs_handler)
 def start(workflow_name,
           service_name,
           inputs,
@@ -140,7 +142,7 @@ def start(workflow_name,
     logger.info('Starting {0}execution. Press Ctrl+C cancel'.format('dry ' if dry else ''))
     execution_thread.start()
 
-    log_consumer = LogConsumer(model_storage, workflow_runner.execution_id)
+    log_consumer = ModelLogConsumer(model_storage, workflow_runner.execution_id)
     try:
         while execution_thread.is_alive():
             for log in log_consumer:

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3cd7111e/aria/cli/commands/logs.py
----------------------------------------------------------------------
diff --git a/aria/cli/commands/logs.py b/aria/cli/commands/logs.py
index 92439ca..b7d86dc 100644
--- a/aria/cli/commands/logs.py
+++ b/aria/cli/commands/logs.py
@@ -12,9 +12,9 @@
 # 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.
-from aria.cli.logger import LogConsumer
-from .. import utils
+from ..logger import ModelLogConsumer
 from ..cli import aria
+from . import custom_logger
 
 
 @aria.group(name='logs')
@@ -29,14 +29,12 @@ def logs():
 @aria.argument('execution-id')
 @aria.options.verbose()
 @aria.pass_model_storage
-@aria.pass_logger
-def list(execution_id,
-         model_storage,
-         logger):
+@aria.pass_logger(log_handler=custom_logger.model_logs_handler)
+def list(execution_id, model_storage, logger):
     """Display logs for an execution
     """
     logger.info('Listing logs for execution id {0}'.format(execution_id))
-    log_consumer = LogConsumer(model_storage, execution_id)
+    log_consumer = ModelLogConsumer(model_storage, execution_id)
     any_logs = False
 
     for log in log_consumer:

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3cd7111e/aria/cli/logger.py
----------------------------------------------------------------------
diff --git a/aria/cli/logger.py b/aria/cli/logger.py
index 24c3374..4e2cd3e 100644
--- a/aria/cli/logger.py
+++ b/aria/cli/logger.py
@@ -18,7 +18,7 @@ import os
 import copy
 import logging
 import logging.config
-
+from contextlib import contextmanager
 
 HIGH_VERBOSE = 3
 MEDIUM_VERBOSE = 2
@@ -52,52 +52,30 @@ DEFAULT_LOGGER_CONFIG = {
 
 
 class _Logger(object):
-    """
-    Enables easy logging of any items written to the storage by an operation (or execution). 
-    """
-    def __init__(self,
-                 base_logger,
-                 model_logger_level,
-                 basic_logger_format=None,
-                 extended_logger_format=None):
-        self._logger = base_logger
-        self.model_logger_level = model_logger_level
-        self._formats = {
-            NO_VERBOSE: basic_logger_format or {},
-            LOW_VERBOSE: extended_logger_format or {},
-        }
+
+    def __init__(self, base_logger, logging, log=None):
+        self._base_logger = base_logger
+        self._logging = logging
+        self._log = log
+
+    @property
+    def logging(self):
+        return self._logging
+
+    @contextmanager
+    def custom_log(self, log=None):
+        original_log = self._log
+        self._log = log
+        yield self
+        self._log = original_log
 
     def log(self, item):
-        # Only NO_VERSBOSE and LOW_VERBOSE are configurable formats. configuring
-        # the low verbose level should affect any higher level.
-        formats = self._formats[min(self.model_logger_level, LOW_VERBOSE)]
-
-        kwargs = dict(item=item)
-        if 'created_at' in formats:
-            kwargs['created_at'] = item.created_at.strftime(formats['created_at'])
-        if 'level' in formats:
-            kwargs['level'] = formats['level'].format(item.level)
-        if 'msg' in formats:
-            kwargs['msg'] = formats['msg'].format(item.msg)
-
-        if 'actor' in formats and item.task:
-            kwargs['actor'] = formats['actor'].format(item.task.actor)
-        if 'execution' in formats:
-            kwargs['execution'] = formats['execution'].format(item.execution)
-
-        # If no format was supplied just print out the original msg.
-        msg = formats.get('main_msg', '{item.msg}').format(**kwargs)
-
-        # Add the exception and the error msg.
-        if item.traceback and self.model_logger_level >= MEDIUM_VERBOSE:
-            msg += os.linesep + '------>'
-            for line in item.traceback.splitlines(True):
-                msg += '\t' + '|' + line
-
-        return getattr(self._logger, item.level.lower())(msg)
+        if self._log is None:
+            raise NotImplementedError
+        return self._log(self.logging, item)
 
     def __getattr__(self, item):
-        return getattr(self._logger, item)
+        return getattr(self._base_logger, item)
 
 
 class Logging(object):
@@ -107,16 +85,7 @@ class Logging(object):
         self._verbosity_level = NO_VERBOSE
         self._all_loggers = []
         self._configure_loggers(config)
-
-        self._lgr = _Logger(
-            base_logger=logging.getLogger('aria.cli.main'),
-            model_logger_level=self._verbosity_level,
-            basic_logger_format={'main_msg': '{item.msg}'},
-            extended_logger_format={
-                    'main_msg': '{created_at} | {item.level[0]} | {item.msg}',
-                    'created_at': '%H:%M:%S'
-                }
-        )
+        self._lgr = _Logger(logging.getLogger('aria.cli.main'), self)
 
     @property
     def logger(self):
@@ -135,7 +104,7 @@ class Logging(object):
 
     @verbosity_level.setter
     def verbosity_level(self, level):
-        self._verbosity_level = self._lgr.model_logger_level = level
+        self._verbosity_level = level
         if self.is_high_verbose_level():
             for logger_name in self._all_loggers:
                 logging.getLogger(logger_name).setLevel(logging.DEBUG)
@@ -171,7 +140,7 @@ class Logging(object):
         logging.config.dictConfig(logger_dict)
 
 
-class LogConsumer(object):
+class ModelLogConsumer(object):
 
     def __init__(self, model_storage, execution_id, filters=None, sort=None):
         self._last_visited_id = 0