From b33bf1210dbb79751bd27f48fb2864f8e2b12aec Mon Sep 17 00:00:00 2001 From: Garrett Michael Flynn Date: Mon, 20 May 2024 13:35:56 -0700 Subject: [PATCH 1/2] Create a log file for each GUIDE run --- pyflask/app.py | 61 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/pyflask/app.py b/pyflask/app.py index d84b27bae..fb9d9a431 100644 --- a/pyflask/app.py +++ b/pyflask/app.py @@ -3,6 +3,7 @@ import json import multiprocessing import sys +from datetime import datetime from logging import DEBUG, Formatter from logging.handlers import RotatingFileHandler from os import getpid, kill @@ -11,6 +12,8 @@ from signal import SIGINT from urllib.parse import unquote +from errorHandlers import notBadRequestException + # https://stackoverflow.com/questions/32672596/pyinstaller-loads-script-multiple-times#comment103216434_32677108 multiprocessing.freeze_support() @@ -21,6 +24,7 @@ from flask_restx import Api, Resource from manageNeuroconv.info import ( CONVERSION_SAVE_FOLDER_PATH, + GUIDE_ROOT_FOLDER, STUB_SAVE_FOLDER_PATH, resource_path, ) @@ -31,20 +35,11 @@ CORS(app) app.config["CORS_HEADERS"] = "Content-Type" -# 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) - +# Create logger configuration +LOG_FOLDER = Path(GUIDE_ROOT_FOLDER, "logs") +LOG_FOLDER.mkdir(exist_ok=True, parents=True) +timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") +LOG_FILE_PATH = Path(LOG_FOLDER, f"{timestamp}.log") # Initialize API package_json_file_path = resource_path("package.json") @@ -115,6 +110,27 @@ def get_species(): return species_map +@api.route("/log") +class Log(Resource): + @api.doc(responses={200: "Success", 400: "Bad Request", 500: "Internal server error"}) + def post(self): + try: + + payload = api.payload + type = payload["type"] + header = payload["header"] + inputs = payload["inputs"] + traceback = payload["traceback"] + + message = f"{header}\n{'-'*len(header)}\n\n{json.dumps(inputs, indent=2)}\n\n{traceback}\n" + selected_logger = getattr(api.logger, type) + selected_logger(message) + + except Exception as exception: + if notBadRequestException(exception): + api.abort(500, str(exception)) + + @api.route("/server_shutdown", endpoint="shutdown") class Shutdown(Resource): def get(self): @@ -131,7 +147,22 @@ def get(self): if __name__ == "__main__": port = sys.argv[len(sys.argv) - 1] if port.isdigit(): + + # Configure logger (avoid reinstantiation for processes) + 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) + + app.logger.info(f"Logging to {LOG_FILE_PATH}") + + # Run the server api.logger.info(f"Starting server on port {port}") app.run(host="127.0.0.1", port=port) else: - raise Exception("No port provided for the NWB GUIDE backend.") + raise Exception("No port provided for the NWB GUIDE backend.") \ No newline at end of file From 7b60aac40c74a01d8e4a5f41575ab7f81b4dce0f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 20 May 2024 20:37:26 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pyflask/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyflask/app.py b/pyflask/app.py index fb9d9a431..a75fb88bb 100644 --- a/pyflask/app.py +++ b/pyflask/app.py @@ -165,4 +165,4 @@ def get(self): api.logger.info(f"Starting server on port {port}") app.run(host="127.0.0.1", port=port) else: - raise Exception("No port provided for the NWB GUIDE backend.") \ No newline at end of file + raise Exception("No port provided for the NWB GUIDE backend.")