From d59302af2ec74b93d601ea6696f28cdef990d7a4 Mon Sep 17 00:00:00 2001 From: Cody Baker Date: Thu, 17 Aug 2023 12:59:52 -0400 Subject: [PATCH] move logging; remove setupUtils; remove minimum API version endpoint and util --- pyflask/apis/startup.py | 7 ------- pyflask/app.py | 21 +++++++++++++++++---- pyflask/setupUtils/__init__.py | 1 - pyflask/setupUtils/configureLogger.py | 25 ------------------------- pyflask/startup/__init__.py | 1 - pyflask/startup/minimumApiVersion.py | 9 --------- 6 files changed, 17 insertions(+), 47 deletions(-) delete mode 100644 pyflask/setupUtils/__init__.py delete mode 100644 pyflask/setupUtils/configureLogger.py delete mode 100644 pyflask/startup/__init__.py delete mode 100644 pyflask/startup/minimumApiVersion.py diff --git a/pyflask/apis/startup.py b/pyflask/apis/startup.py index e1f661666..3824a9465 100644 --- a/pyflask/apis/startup.py +++ b/pyflask/apis/startup.py @@ -1,5 +1,4 @@ """API endpoint definitions for startup operations.""" -from startup import get_api_version from flask_restx import Namespace, Resource startup_api = Namespace("startup", description="API for startup commands related to the NWB GUIDE.") @@ -20,9 +19,3 @@ class Echo(Resource): def get(self): args = parser.parse_args() return args["arg"] - - -@startup_api.route("/minimum_api_version") -class MinimumApiVersion(Resource): - def get(self): - return get_api_version() diff --git a/pyflask/app.py b/pyflask/app.py index 8454c58d8..bf79cb131 100644 --- a/pyflask/app.py +++ b/pyflask/app.py @@ -2,6 +2,8 @@ import sys import json import multiprocessing +from logging import Formatter, DEBUG +from logging.handlers import RotatingFileHandler from pathlib import Path from flask import Flask, request, send_from_directory @@ -9,7 +11,6 @@ from flask_restx import Api, Resource from apis import startup_api, neuroconv_api -from setupUtils import configureLogger from manageNeuroconv.info import STUB_SAVE_FOLDER_PATH, CONVERSION_SAVE_FOLDER_PATH app = Flask(__name__) @@ -18,8 +19,22 @@ CORS(app) app.config["CORS_HEADERS"] = "Content-Type" -configureLogger(app) +# Configure logger +LOG_FILE_PATH = Path.home() / "NWB_GUIDE" / "logs" / "api.log" +LOG_FILE_PATH.parent.mkdir(parents=True, exist_ok=True) +log_handler = RotatingFileHandler(LOG_FILE_PATH, maxBytes=5 * 1024 * 1024, backupCount=3) +log_formatter = Formatter( + fmt="%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s: %(message)s", + datefmt="%Y-%m-%d %H:%M:%S", +) +log_handler.setFormatter(log_formatter) + +app.logger.addHandler(log_handler) +app.logger.setLevel(DEBUG) + + +# Initialize API package_json_file_path = Path(__file__).parent.parent / "package.json" with open(file=package_json_file_path) as fp: package_json = json.load(fp=fp) @@ -29,10 +44,8 @@ title="NWB GUIDE API", description="The REST API for the NWB GUIDE provided by the Python Flask Server.", ) - api.add_namespace(startup_api) api.add_namespace(neuroconv_api) - api.init_app(app) diff --git a/pyflask/setupUtils/__init__.py b/pyflask/setupUtils/__init__.py deleted file mode 100644 index d59f8bcb7..000000000 --- a/pyflask/setupUtils/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .configureLogger import configureLogger diff --git a/pyflask/setupUtils/configureLogger.py b/pyflask/setupUtils/configureLogger.py deleted file mode 100644 index 57b52c31b..000000000 --- a/pyflask/setupUtils/configureLogger.py +++ /dev/null @@ -1,25 +0,0 @@ -from os import makedirs, path -import logging -import logging.handlers - - -def configureLogger(app): - LOG_FOLDER = path.join(path.expanduser("~"), "NWB_GUIDE", "logs") - LOG_FILENAME = "api.log" - LOG_PATH = path.join(LOG_FOLDER, LOG_FILENAME) - - if not path.exists(LOG_FOLDER): - makedirs(LOG_FOLDER) - - # Add the log message handler to the logger - handler = logging.handlers.RotatingFileHandler(LOG_PATH, maxBytes=5 * 1024 * 1024, backupCount=3) - - # create logging formatter - logFormatter = logging.Formatter( - fmt="%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s: %(message)s", - datefmt="%Y-%m-%d %H:%M:%S", - ) - handler.setFormatter(logFormatter) - - app.logger.addHandler(handler) - app.logger.setLevel(logging.DEBUG) diff --git a/pyflask/startup/__init__.py b/pyflask/startup/__init__.py deleted file mode 100644 index 7e37c612a..000000000 --- a/pyflask/startup/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .minimumApiVersion import get_api_version diff --git a/pyflask/startup/minimumApiVersion.py b/pyflask/startup/minimumApiVersion.py deleted file mode 100644 index 007382cbb..000000000 --- a/pyflask/startup/minimumApiVersion.py +++ /dev/null @@ -1,9 +0,0 @@ -import os - - -def get_api_version(): - """ - Returns the version of the API - """ - - return {"version": os.getenv("API_VERSION", "9.4.1")}