Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a log file for each GUIDE run #787

Merged
merged 2 commits into from
May 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions pyflask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand All @@ -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,
)
Expand All @@ -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")
Expand Down Expand Up @@ -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):
Expand All @@ -131,6 +147,21 @@ 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:
Expand Down
Loading