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/02/19 17:03:24 UTC

[6/7] incubator-ariatosca git commit: added source to log

added source to log


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

Branch: refs/heads/ARIA-106-Create-sqla-logging-handler
Commit: c3f4501d45006b207211fd8b957f12ca966b395d
Parents: f881d0b
Author: mxmrlv <mx...@gmail.com>
Authored: Mon Feb 13 23:03:54 2017 +0200
Committer: mxmrlv <mx...@gmail.com>
Committed: Sun Feb 19 10:38:30 2017 +0200

----------------------------------------------------------------------
 aria/logger.py                                 | 14 +++++++-------
 aria/orchestrator/context/common.py            | 17 +++++++++++++++--
 aria/orchestrator/context/operation.py         |  8 ++++++++
 aria/orchestrator/context/workflow.py          |  1 +
 aria/storage/modeling/orchestrator_elements.py |  4 +++-
 tests/orchestrator/context/test_operation.py   | 19 ++++++++++---------
 6 files changed, 44 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c3f4501d/aria/logger.py
----------------------------------------------------------------------
diff --git a/aria/logger.py b/aria/logger.py
index ebe01ba..6940d43 100644
--- a/aria/logger.py
+++ b/aria/logger.py
@@ -138,7 +138,11 @@ def create_file_log_handler(
 
 
 class SQLAlchemyHandler(logging.Handler):
+
     def __init__(self, session, engine, log_cls, **kwargs):
+        from aria.storage.modeling import instance_elements
+
+        self._instance_elements = instance_elements
         self._session = session
         self._engine = engine
         self._cls = log_cls
@@ -146,10 +150,11 @@ class SQLAlchemyHandler(logging.Handler):
 
     def emit(self, record):
         log = self._cls(
+            prefix=record.prefix,
             logger=record.name,
             level=record.levelname,
             msg=record.msg,
-            created_at=datetime.utcnow()
+            created_at=datetime.utcnow(),
         )
         self._session.add(log)
         self._session.commit()
@@ -163,15 +168,10 @@ class _SQLAlchemyHandlerFactory(object):
         self._handler = None
 
     def __call__(self, session, engine, model_cls=Log, level=logging.DEBUG):
-        if self._handler is None or not self._is_eq(session, engine, model_cls):
+        if not self._handler:
             self._handler = SQLAlchemyHandler(session, engine, model_cls, level=level)
         return self._handler
 
-    def _is_eq(self, session, engine, model_cls):
-        return all([self._handler._session == session,
-                    self._handler._engine == engine,
-                    self._handler._cls == model_cls])
-
 create_sqla_log_handler = _SQLAlchemyHandlerFactory()
 
 _default_file_formatter = logging.Formatter(

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c3f4501d/aria/orchestrator/context/common.py
----------------------------------------------------------------------
diff --git a/aria/orchestrator/context/common.py b/aria/orchestrator/context/common.py
index d5ef42c..7e84c0c 100644
--- a/aria/orchestrator/context/common.py
+++ b/aria/orchestrator/context/common.py
@@ -15,6 +15,7 @@
 """
 A common context for both workflow and operation
 """
+from functools import partial
 from uuid import uuid4
 
 import logging
@@ -25,6 +26,18 @@ from aria import logger
 from aria.storage import exceptions
 
 
+class _Logger(object):
+    def __init__(self, logger):
+        self._logger = logger
+        self._prefix = ''
+
+    def set_prefix(self, prefix):
+        self._prefix = prefix
+
+    def __getattr__(self, item):
+        return partial(getattr(self._logger, item), extra={'prefix': self._prefix})
+
+
 class BaseContext(object):
     """
     Base context object for workflow and operation
@@ -45,7 +58,7 @@ class BaseContext(object):
         self._model = model_storage
         self._resource = resource_storage
         self._service_instance_id = service_instance_id
-        self._logger = self._init_logger(ctx_logger)
+        self._logger = self._init_logger(ctx_logger=ctx_logger)
         self._workdir = workdir
 
     def _init_logger(self, ctx_logger=None):
@@ -58,7 +71,7 @@ class BaseContext(object):
 
         ctx_logger.setLevel(logging.DEBUG)
 
-        return ctx_logger
+        return _Logger(ctx_logger)
 
     def __repr__(self):
         return (

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c3f4501d/aria/orchestrator/context/operation.py
----------------------------------------------------------------------
diff --git a/aria/orchestrator/context/operation.py b/aria/orchestrator/context/operation.py
index c5ac8f0..efe62ab 100644
--- a/aria/orchestrator/context/operation.py
+++ b/aria/orchestrator/context/operation.py
@@ -105,6 +105,10 @@ class NodeOperationContext(BaseOperationContext):
     """
     Context for node based operations.
     """
+    def __init__(self, **kwargs):
+        super(NodeOperationContext, self).__init__(**kwargs)
+        self.logger.set_prefix(str(self.node))
+
     @property
     def node_template(self):
         """
@@ -126,6 +130,10 @@ class RelationshipOperationContext(BaseOperationContext):
     """
     Context for relationship based operations.
     """
+    def __init__(self, **kwargs):
+        super(RelationshipOperationContext, self).__init__(**kwargs)
+        self.logger.set_prefix(str(self.relationship))
+
     @property
     def source_node_template(self):
         """

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c3f4501d/aria/orchestrator/context/workflow.py
----------------------------------------------------------------------
diff --git a/aria/orchestrator/context/workflow.py b/aria/orchestrator/context/workflow.py
index 00ed974..297e1bc 100644
--- a/aria/orchestrator/context/workflow.py
+++ b/aria/orchestrator/context/workflow.py
@@ -46,6 +46,7 @@ class WorkflowContext(BaseContext):
         # TODO: execution creation should happen somewhere else
         # should be moved there, when such logical place exists
         self._execution_id = self._create_execution() if execution_id is None else execution_id
+        self.logger.set_prefix(str(self.execution))
 
     def __repr__(self):
         return (

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c3f4501d/aria/storage/modeling/orchestrator_elements.py
----------------------------------------------------------------------
diff --git a/aria/storage/modeling/orchestrator_elements.py b/aria/storage/modeling/orchestrator_elements.py
index 8efc147..edd40b9 100644
--- a/aria/storage/modeling/orchestrator_elements.py
+++ b/aria/storage/modeling/orchestrator_elements.py
@@ -475,6 +475,8 @@ class LogBase(ModelMixin):
     level = Column(String)
     msg = Column(String)
     created_at = Column(DateTime, index=True)
+    prefix = Column(String)
 
     def __repr__(self):
-        return "<Log: {0} - {1}>".format(self.created_at, self.msg[:50])
+        return "<Log [{self.prefix}]: {self.created_at} - {msg}>".format(
+            self=self, msg=self.msg[:50])

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/c3f4501d/tests/orchestrator/context/test_operation.py
----------------------------------------------------------------------
diff --git a/tests/orchestrator/context/test_operation.py b/tests/orchestrator/context/test_operation.py
index 08d360c..c216a16 100644
--- a/tests/orchestrator/context/test_operation.py
+++ b/tests/orchestrator/context/test_operation.py
@@ -47,7 +47,9 @@ def ctx(tmpdir):
 
 @pytest.fixture
 def executor():
-    result = thread.ThreadExecutor()
+    from aria.orchestrator.workflows.executor import process
+    result = process.ProcessExecutor()
+    # result = thread.ThreadExecutor()
     try:
         yield result
     finally:
@@ -242,25 +244,24 @@ def test_operation_logging(ctx, executor):
 
     execute(workflow_func=basic_workflow, workflow_context=ctx, executor=executor)
 
-    op_start_log = ctx.model.log.list(filters=dict(msg=inputs['op_start']))
+    logs = ctx.model.log.list()
+    assert len(logs) == 6
+
+    op_start_log = [l for l in logs if inputs['op_start'] in l.msg and l.level.lower() == 'info']
     assert len(op_start_log) == 1
     op_start_log = op_start_log[0]
-    assert op_start_log.level.lower() == 'info'
 
-    op_end_log = ctx.model.log.list(filters=dict(msg=inputs['op_end']))
+    op_end_log = [l for l in logs if inputs['op_end'] in l.msg and l.level.lower() == 'debug']
     assert len(op_end_log) == 1
     op_end_log = op_end_log[0]
-    assert op_end_log.level.lower() == 'debug'
 
-    wf_start_log = ctx.model.log.list(filters=dict(msg=wf_start))
+    wf_start_log = [l for l in logs if wf_start in l.msg and l.level.lower() == 'info']
     assert len(wf_start_log) == 1
     wf_start_log = wf_start_log[0]
-    assert wf_start_log.level.lower() == 'info'
 
-    wf_end_log = ctx.model.log.list(filters=dict(msg=wf_end))
+    wf_end_log = [l for l in logs if wf_end in l.msg and l.level.lower() == 'debug']
     assert len(wf_end_log) == 1
     wf_end_log = wf_end_log[0]
-    assert wf_end_log.level.lower() == 'debug'
 
     assert (wf_start_log.created_at <
             wf_end_log.created_at <