From 6425ac7842eec794840206112ae5aedbef61710c Mon Sep 17 00:00:00 2001 From: Matthew Bourque Date: Mon, 20 Mar 2017 17:06:13 -0400 Subject: [PATCH 1/2] Added a function to create a log file to log the execution of a module. --- acsql/utils/utils.py | 52 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/acsql/utils/utils.py b/acsql/utils/utils.py index 40b6302..2735422 100644 --- a/acsql/utils/utils.py +++ b/acsql/utils/utils.py @@ -1,5 +1,6 @@ -"""This module contains a function that reads in the acsql config file -and returns its settings. +"""This module contains several functions that are useful to various +modules within the acsql package. See individual function docstrings +for further information. Authors ------- @@ -12,6 +13,7 @@ various acsql modules and scripts, as such: from acsql.utils.utils import SETTINGS + from acsql.utils.utils import setup_logging Dependencies ------------ @@ -20,9 +22,18 @@ (1) sqlalchemy """ +import datetime +import getpass +import logging import os +import socket +import sys import yaml +import astropy +import numpy +import sqlalchemy + __config__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))) @@ -43,3 +54,40 @@ def get_settings(): SETTINGS = get_settings() + + +def setup_logging(module): + """Configures a log file that logs the execution of the given + module. Log files are written to the log_dir that is set in the + config.yaml configuration file. The filename of the log file is + _.log. + + Parameters + ---------- + module : str + The name of the module to log. + """ + + SETTINGS = get_settings() + + # Configure logging + timestamp = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M') + filename = '{0}_{1}.log'.format(module, timestamp) + logfile = os.path.join(SETTINGS['log_dir'], filename) + logging.basicConfig( + filename=logfile, + format='%(asctime)s %(levelname)s: %(message)s', + datefmt='%m/%d/%Y %H:%M:%S', + level=logging.INFO) + + # Log environment information + logging.info('User: {0}'.format(getpass.getuser())) + logging.info('System: {0}'.format(socket.gethostname())) + logging.info('Python Version: {0}'.format(sys.version.replace('\n', ''))) + logging.info('Python Path: {0}'.format(sys.executable)) + logging.info('Numpy Version: {0}'.format(numpy.__version__)) + logging.info('Numpy Path: {0}'.format(numpy.__path__[0])) + logging.info('Astropy Version: {0}'.format(astropy.__version__)) + logging.info('Astropy Path: {0}'.format(astropy.__path__[0])) + logging.info('SQLAlchemy Version: {0}'.format(sqlalchemy.__version__)) + logging.info('SQLAlchemy Path: {0}'.format(sqlalchemy.__path__[0])) From cb8b5b8caf96ccd94db8d7b53e5d0f8e1487cc63 Mon Sep 17 00:00:00 2001 From: Matthew Bourque Date: Mon, 20 Mar 2017 17:06:29 -0400 Subject: [PATCH 2/2] Added logging of ingestion script. --- acsql/ingest/ingest.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/acsql/ingest/ingest.py b/acsql/ingest/ingest.py index d65bd40..01fe9f1 100644 --- a/acsql/ingest/ingest.py +++ b/acsql/ingest/ingest.py @@ -20,6 +20,7 @@ from acsql.database.database_interface import Master from acsql.database.database_interface import session from acsql.utils.utils import SETTINGS +from acsql.utils.utils import setup_logging def get_files_to_ingest(): @@ -49,9 +50,10 @@ def ingest(): files_to_ingest = get_files_to_ingest() - print(len(files_to_ingest)) - if __name__ == '__main__': - ingest() \ No newline at end of file + module = os.path.basename(__file__).strip('.py') + setup_logging(module) + + ingest()