From ba85e67111bd3cb9dde8b42d517efd41a82be7dc Mon Sep 17 00:00:00 2001 From: James Eddy Date: Mon, 15 Jul 2019 11:21:01 -0700 Subject: [PATCH] add more logging, use logger consistently --- synprov/__init__.py | 11 ++++++++++- synprov/__main__.py | 13 +++++++------ synprov/config.py | 8 ++++++-- synprov/mock/main.py | 15 ++++++++------- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/synprov/__init__.py b/synprov/__init__.py index 94be949..55d794f 100644 --- a/synprov/__init__.py +++ b/synprov/__init__.py @@ -1,11 +1,20 @@ +import logging import connexion from healthcheck import HealthCheck +from synprov.config import connex_app from synprov.config import neo4j_connection + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + + def neo4j_available(): try: + logger.info("Checking connection at {}" + .format(neo4j_connection.database.uri)) neo4j_connection.run('MATCH () RETURN 1 LIMIT 1') return True, "neo4j ok" except AttributeError: @@ -13,7 +22,7 @@ def neo4j_available(): def create_app(): - app = config.connex_app + app = connex_app app.add_api('openapi.yaml', arguments={'title': 'Provenance Service'}) # wrap the flask app and give a heathcheck url health = HealthCheck(app, "/healthcheck") diff --git a/synprov/__main__.py b/synprov/__main__.py index f416407..d51cfef 100644 --- a/synprov/__main__.py +++ b/synprov/__main__.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import connexion +import logging from synprov import create_app from synprov.config import neo4j_connection as graph @@ -8,13 +9,13 @@ from synprov.mock.main import create_mock_graph +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + + def init_db(num_activities=30): - graph.run( - ''' - MATCH (n) - DETACH DELETE n - ''' - ) + graph.delete_all() + logger.info("Populating mock graph") create_mock_graph(GraphClient(graph), num_activities) diff --git a/synprov/config.py b/synprov/config.py index 47c6ba1..4097833 100644 --- a/synprov/config.py +++ b/synprov/config.py @@ -1,20 +1,24 @@ #!/usr/bin/env python3 - +import logging import os import connexion from py2neo import Graph +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + connex_app = connexion.App(__name__, specification_dir='./openapi/') env_host = os.environ.get('NEO4J_HOST') -neo4j_host = env_host if env_host is not None else 'localhost' +neo4j_host = env_host if env_host is not None else '0.0.0.0' print(neo4j_host) neo_user = os.environ['NEO4J_USERNAME'] neo_pass = os.environ['NEO4J_PASSWORD'] +logger.info("Connecting to neo4j database") neo4j_connection = Graph( scheme='bolt', host=neo4j_host, diff --git a/synprov/mock/main.py b/synprov/mock/main.py index de63409..92815b1 100644 --- a/synprov/mock/main.py +++ b/synprov/mock/main.py @@ -11,9 +11,10 @@ from synprov.graph.client import GraphClient -logging.basicConfig(format='%(asctime)s | %(levelname)s : %(message)s', - level=logging.INFO, - stream=sys.stdout) +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + + matcher = NodeMatcher(graph) # ------------------------------ @@ -38,11 +39,11 @@ def add_activities(kt): return x -def create_mock_graph(graph_db, NUMACTIVITIES): +def create_mock_graph(graph_client, NUMACTIVITIES): # SCRIPT # step 1 - Create activities: - logging.info("Generating table of Activities...") + logger.info("Generating table of Activities...") activity_array = add_activities(NUMACTIVITIES) # step 2 - Create corresponding nodes and relationships @@ -50,9 +51,9 @@ def create_mock_graph(graph_db, NUMACTIVITIES): ref_num = 0 agt_num = 0 for i in activity_array: - logging.info("Building Activity '{}' of class '{}'" + logger.info("Building Activity '{}' of class '{}'" .format(i.name, i._class)) - act = ActivityMocker(graph_db, i, ref_num, agt_num) + act = ActivityMocker(graph_client, i, ref_num, agt_num) (rn, an) = act.save() ref_num += rn agt_num += an