Skip to content

Commit

Permalink
Add a common logger module for logging in different modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
rupav committed Jul 18, 2018
1 parent 81dd57d commit 4fb94cb
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 18 deletions.
5 changes: 4 additions & 1 deletion candis/app/server/api/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
from candis.app.server.models.pipeline import Pipeline, Cdata
from candis.app.server.models.user import User
from candis.app.server.helpers.fileData import modify_data_path
from logger import get_logger

log = get_logger()

FFORMATS = JSON.read(os.path.join(R.Path.DATA, 'file-formats.json'))
ABSPATH_STARTDIR = os.path.abspath(CONFIG.App.STARTDIR)
Expand Down Expand Up @@ -101,7 +104,7 @@ def discover_resource(path, level = None, filter_ = None):
return tree

def log_times(i):
print("No. of times tried to connect to NCBI: {}".format(i))
log.info("No. of times tried to connect to NCBI: {}".format(i))

@app.route(CONFIG.App.Routes.API.Data.RESOURCE, methods = ['GET', 'POST'])
@login_required
Expand Down
5 changes: 3 additions & 2 deletions candis/app/server/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# imports - standard imports
import os
import sys
import logging
# import logging

# imports - third-party imports
from flask import Flask
Expand All @@ -17,8 +17,9 @@
from candis.config import CONFIG
from candis.resource import R
from candis.manager.redis.redis import Redis
from logger import get_logger

log = logging.getLogger(__name__)
log = get_logger()

app = Flask(__name__,
template_folder = R.Path.TEMPLATES,
Expand Down
10 changes: 6 additions & 4 deletions candis/app/server/models/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

# imports - module imports
from candis.app.server.app import db
from logger import get_logger()

log = get_logger()

class Pipeline(db.Model):
__tablename__ = 'pipeline'
Expand All @@ -24,7 +26,7 @@ def add_pipeline(self):
except Exception as e:
db.session.rollback()
db.session.flush()
print(e) # use logging
log.error(e)

def delete_pipeline(self):
try:
Expand All @@ -33,7 +35,7 @@ def delete_pipeline(self):
except Exception as e:
db.session.rollback()
db.session.flush()
print(e) # use logging
log.error(e)


def update_pipeline(self, **kwargs):
Expand Down Expand Up @@ -68,7 +70,7 @@ def add_pipeline_run(self):
except Exception as e:
db.session.rollback()
db.session.flush()
print(e) # use logging
log.error(e)

class Cdata(db.Model):
__tablename__ = 'cdata'
Expand All @@ -85,7 +87,7 @@ def add_cdata(self):
except Exception as e:
db.session.rollback()
db.session.flush()
print(e) # TODO: use logging
log.error(e)

@classmethod
def get_cdata(cls, id_=None, name=None):
Expand Down
5 changes: 4 additions & 1 deletion candis/app/server/models/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

# imports - module imports
from candis.app.server.app import db
from logger import get_logger()

log = get_logger()

class Response(db.Model):
__tablename__ = 'response'
Expand All @@ -25,7 +28,7 @@ def add_response(self):
except Exception as e:
db.session.rollback()
db.session.flush()
print(e) # use logging
log.error(e)

@classmethod
def get_response(cls, id=None, version=None, status=None):
Expand Down
7 changes: 5 additions & 2 deletions candis/app/server/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

# imports - module imports
from candis.app.server.app import db
from logger import get_logger()

log = get_logger()

class User(db.Model):
__tablename__ = 'users'
Expand All @@ -26,7 +29,7 @@ def add_user(self):
except Exception as e:
db.session.rollback()
db.session.flush()
print(e) # use logging
log.error(e)

@classmethod
def get_user(cls, id_=None, username=None, email=None):
Expand All @@ -46,7 +49,7 @@ def delete_user(cls, id_=None, username=None, email=None):
except Exception as e:
db.session.rollback()
db.session.flush()
print(e) # use logging
log.error(e)

def _encrypt(self, pswd):
return generate_password_hash(pswd)
Expand Down
13 changes: 8 additions & 5 deletions candis/data/GEO/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

# imports - module imports
from candis.util import assign_if_none
from logger import get_logger()

log = get_logger()

class API():
def __init__(self, path='', ftype='suppl'):
Expand All @@ -24,7 +27,7 @@ def _ftp_connect(self, host, usr=None, pswd=None):
def _ftp_close(self):
if isinstance(self.ftp, FTP):
self.ftp.quit()
print("Closed successfully!")
log.info("Closed successfully!")
self.ftp = None

def raw_data(self, ftp_link, series_accession, path=None):
Expand All @@ -50,12 +53,12 @@ def raw_data(self, ftp_link, series_accession, path=None):
file_path = os.path.join(self.path, tar_file)

with open(file_path, 'wb') as f:
print("\n Downloading {} at {} \n".format(tar_file, os.path.abspath(self.path)))
log.info("\n Downloading {} at {} \n".format(tar_file, os.path.abspath(self.path)))
try:
self.ftp.retrbinary('RETR '+ file_name, f.write)
print("Downloaded!")
log.info("Downloaded!")
except EOFError:
print("Connection closed, couldn't download")
log.error("Connection closed, couldn't download")
except Exception as e:
print("Error {}".format(e))
log.error("Error {}".format(e))
self._ftp_close()
7 changes: 5 additions & 2 deletions candis/data/entrez/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
from candis.util import validate_email
from candis.config import CONFIG
from candis.manager import Redis
from logger import get_logger()

log = get_logger()

def sanitize_response(response, type_ = 'json'):
if type_ == 'json':
Expand Down Expand Up @@ -71,7 +74,7 @@ def __init__(self, email, name = None, api_key = None):
except ResponseError:
# exception caught is custom ResponseError of redis.
# TODO: instead of raising exception, give a warning or use logging.captureWarning or log INFO
print('redis key "databases" must be a list, refreshing cache.')
log.info('redis key "databases" must be a list, refreshing cache.')
else:
return None

Expand Down Expand Up @@ -168,7 +171,7 @@ def summary(self, db = 'pubmed', id = [], **optional):
if(optional.get('query_key') and optional.get('WebEnv')):
if db not in self.databases:
raise ValueError('database should be from : {}'.format(self.databases))
# print("Using WebEnv and query_key") - TODO: Use logging instead
log.info("Using WebEnv and query_key")
optional.update({'db': db})
data = self.request('get', URL.SUMMARY, optional)
return data
Expand Down
5 changes: 4 additions & 1 deletion candis/ios/cdata/cdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
from candis.resource import R
from candis.util import assign_if_none
from candis.ios import json as JSON
from logger import get_logger()

log = get_logger()

ATTRIBUTE_TYPES = JSON.read(os.path.join(R.Path.DATA, 'attribute-types.json'))

Expand Down Expand Up @@ -262,7 +265,7 @@ def to_json(self, buffer_):
cnames.append(cname)
buffer_.update({"cnames": cnames})
except Exception as e:
print(str(e))
log.error(str(e))

def to_dict(self):
data = self.data.copy()
Expand Down
23 changes: 23 additions & 0 deletions logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# imports - standard imports
import logging

def get_logger(name=None, logfile=None):
logging.basicConfig()
logger = logging.getLogger(name or __name__)
logger.setLevel(logging.DEBUG)
logger.propagate = False

if not logger.handlers:
formatter = logging.Formatter("%(asctime)s | {%(pathname)s:%(lineno)d - %(funcName)s} %(levelname)s %(message)s", "%H:%M:%S")

fileHandler = logging.FileHandler(logfile or 'candis.log', mode='a')
fileHandler.setFormatter(formatter)
logger.addHandler(fileHandler)

formatter = logging.Formatter("%(asctime)s | %(message)s", "%H:%M:%S")

handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)

return logger

0 comments on commit 4fb94cb

Please sign in to comment.