ariatosca-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mxm...@apache.org
Subject incubator-ariatosca git commit: wip
Date Sun, 12 Feb 2017 12:31:10 GMT
Repository: incubator-ariatosca
Updated Branches:
  refs/heads/ARIA-106-Create-sqla-logging-handler [created] 51c70c86a


wip


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

Branch: refs/heads/ARIA-106-Create-sqla-logging-handler
Commit: 51c70c86a92a4614eff858951cfdae7154e6c58a
Parents: 1498ad3
Author: mxmrlv <mxmrlv@gmail.com>
Authored: Sun Feb 12 14:30:26 2017 +0200
Committer: mxmrlv <mxmrlv@gmail.com>
Committed: Sun Feb 12 14:30:26 2017 +0200

----------------------------------------------------------------------
 aria/logger.py             | 24 +++++++++++++++++++
 aria/storage/__init__.py   |  2 +-
 aria/storage/base_model.py | 13 +++++++++++
 aria/storage/model.py      |  4 ++++
 tests/storage/__init__.py  |  3 +--
 tests/test_logger.py       | 51 ++++++++++++++++++++++++++++++++++++++++-
 6 files changed, 93 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/51c70c86/aria/logger.py
----------------------------------------------------------------------
diff --git a/aria/logger.py b/aria/logger.py
index 0002cb5..854a8bd 100644
--- a/aria/logger.py
+++ b/aria/logger.py
@@ -135,5 +135,29 @@ def create_file_log_handler(
     return rotating_file
 
 
+def create_sqla_log_handler(session, engine, level=logging.DEBUG):
+    from aria.storage.model import Log
+    return SQLAlchemyHandler(session, engine, Log, level=level)
+
+
 _default_file_formatter = logging.Formatter(
     '%(asctime)s [%(name)s:%(levelname)s] %(message)s <%(pathname)s:%(lineno)d>')
+
+
+class SQLAlchemyHandler(logging.Handler):
+    def __init__(self, session, engine, log_cls, **kwargs):
+        self._session = session
+        self._engine = engine
+        self._cls = log_cls
+        super(SQLAlchemyHandler, self).__init__(**kwargs)
+
+    def emit(self, record):
+        import datetime
+        log = self._cls(
+            logger=record.name,
+            level=record.levelname,
+            msg=record.msg,
+            created_at=datetime.datetime.now()
+        )
+        self._session.add(log)
+        self._session.commit()

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/51c70c86/aria/storage/__init__.py
----------------------------------------------------------------------
diff --git a/aria/storage/__init__.py b/aria/storage/__init__.py
index a1c07d7..a51f0b6 100644
--- a/aria/storage/__init__.py
+++ b/aria/storage/__init__.py
@@ -49,7 +49,7 @@ from . import (
     core,
     filesystem_rapi,
     sql_mapi,
-    model
+    model,
 )
 
 __all__ = (

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/51c70c86/aria/storage/base_model.py
----------------------------------------------------------------------
diff --git a/aria/storage/base_model.py b/aria/storage/base_model.py
index f7d0e5b..5d06fea 100644
--- a/aria/storage/base_model.py
+++ b/aria/storage/base_model.py
@@ -50,6 +50,7 @@ from sqlalchemy import (
     String,
     Float,
     orm,
+    func
 )
 from sqlalchemy.ext.orderinglist import ordering_list
 
@@ -755,3 +756,15 @@ class TaskBase(ModelMixin):
     @staticmethod
     def retry(message=None, retry_interval=None):
         raise TaskRetryException(message, retry_interval=retry_interval)
+
+
+class LogBase(ModelMixin):
+    __tablename__ = 'log'
+
+    logger = Column(String)
+    level = Column(String)
+    msg = Column(String)
+    created_at = Column(DateTime)
+
+    def __repr__(self):
+        return "<Log: {0} - {1}>".format(self.created_at, self.msg[:50])

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/51c70c86/aria/storage/model.py
----------------------------------------------------------------------
diff --git a/aria/storage/model.py b/aria/storage/model.py
index afca3e4..23fbb88 100644
--- a/aria/storage/model.py
+++ b/aria/storage/model.py
@@ -108,3 +108,7 @@ class Plugin(DeclarativeBase, base.PluginBase):
 
 class Task(DeclarativeBase, base.TaskBase):
     pass
+
+
+class Log(DeclarativeBase, base.LogBase):
+    pass
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/51c70c86/tests/storage/__init__.py
----------------------------------------------------------------------
diff --git a/tests/storage/__init__.py b/tests/storage/__init__.py
index b798e01..2ba6da9 100644
--- a/tests/storage/__init__.py
+++ b/tests/storage/__init__.py
@@ -12,8 +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
-import platform
+
 from tempfile import mkdtemp
 from shutil import rmtree
 

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/51c70c86/tests/test_logger.py
----------------------------------------------------------------------
diff --git a/tests/test_logger.py b/tests/test_logger.py
index 70f08bb..0a4ab37 100644
--- a/tests/test_logger.py
+++ b/tests/test_logger.py
@@ -20,7 +20,12 @@ from aria.logger import (create_logger,
                          create_file_log_handler,
                          _default_file_formatter,
                          LoggerMixin,
-                         _DefaultConsoleFormat)
+                         _DefaultConsoleFormat,
+                         create_sqla_log_handler)
+from aria import application_model_storage
+from aria.storage import ModelStorage, sql_mapi
+
+from .storage import init_inmemory_model_storage, model
 
 def test_create_logger():
 
@@ -37,6 +42,50 @@ def test_create_logger():
     assert logger.level == logging.INFO
 
 
+def test_create_sqla_log_handler():
+    debug_test_string = 'debug_create_console_test_string'
+    info_test_string = 'info_create_console_test_string'
+
+    storage = ModelStorage(api_cls=sql_mapi.SQLAlchemyModelAPI,
+                           initiator=init_inmemory_model_storage,
+                           items=[model.Log])
+
+    handler = create_sqla_log_handler(**storage._additional_api_kwargs)
+
+    logger = create_logger(handlers=[handler])
+
+    # First logging
+    assert len(storage.log.list()) == 1
+    creation_string = storage.log.list()[0].msg
+    import time
+    time.sleep(2)
+    logger.info(info_test_string)
+    time.sleep(2)
+    logger.debug(debug_test_string)
+
+    logs = storage.log.list()
+
+    assert len(logs) == 3
+    info_logs = filter(lambda l: l.level.lower() == 'info', logs)
+    debugs_logs = filter(lambda l: l.level.lower() == 'debug', logs)
+
+    assert len(info_logs) == 1
+    info_log = info_logs[0]
+    assert info_log.msg == info_test_string
+
+    assert len(debugs_logs) == 2
+    creation_logs = filter(lambda l: l.msg == creation_string, debugs_logs)
+    assert len(creation_logs) == 1
+    debug_creation_log = creation_logs[0]
+
+    debug_logs = filter(lambda l: l.msg == debug_test_string, debugs_logs)
+    assert len(debug_logs) == 1
+    debug_log = debugs_logs[0]
+
+    assert debug_creation_log.created_at < info_log.created_at < debug_log.created_at
+
+
+
 def test_create_console_log_handler(capsys):
 
     debug_test_string = 'debug_create_console_test_string'


Mime
View raw message