Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 916F8200C19 for ; Sun, 12 Feb 2017 13:31:16 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 90003160B6B; Sun, 12 Feb 2017 12:31:16 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id B5B77160B45 for ; Sun, 12 Feb 2017 13:31:15 +0100 (CET) Received: (qmail 93952 invoked by uid 500); 12 Feb 2017 12:31:14 -0000 Mailing-List: contact dev-help@ariatosca.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ariatosca.incubator.apache.org Delivered-To: mailing list dev@ariatosca.incubator.apache.org Received: (qmail 93941 invoked by uid 99); 12 Feb 2017 12:31:14 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 12 Feb 2017 12:31:14 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id 2E48E180684 for ; Sun, 12 Feb 2017 12:31:14 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -5.22 X-Spam-Level: X-Spam-Status: No, score=-5.22 tagged_above=-999 required=6.31 tests=[HK_RANDOM_FROM=0.999, KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-2.999] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id 8jyiQtTa35bD for ; Sun, 12 Feb 2017 12:31:12 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id 0438E5F403 for ; Sun, 12 Feb 2017 12:31:10 +0000 (UTC) Received: (qmail 93937 invoked by uid 99); 12 Feb 2017 12:31:10 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 12 Feb 2017 12:31:10 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 2566ADFCBD; Sun, 12 Feb 2017 12:31:10 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: mxmrlv@apache.org To: dev@ariatosca.incubator.apache.org Message-Id: <57a7ff4d5a6a4f93a707cd7bfb4bf4e7@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: incubator-ariatosca git commit: wip Date: Sun, 12 Feb 2017 12:31:10 +0000 (UTC) archived-at: Sun, 12 Feb 2017 12:31:16 -0000 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 Authored: Sun Feb 12 14:30:26 2017 +0200 Committer: mxmrlv 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 "".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'