11# Standard library imports
22import argparse
3+ import logging
34import os
45
56# Third-party imports
1617api = Api (app )
1718db = SQLAlchemy (app )
1819
20+ # Configure logging
21+ logging .basicConfig (level = logging .INFO )
22+ logger = logging .getLogger (__name__ )
23+
1924# Add CORS headers
2025@app .after_request
2126def after_request (response ):
@@ -66,25 +71,25 @@ def serve_frontend(path):
6671 path = path .lstrip ('/' )
6772
6873 # Debug logging
69- print (f"Requested path: '{ path } '" )
74+ logger . debug (f"Requested path: '{ path } '" )
7075
7176 # If path is empty, serve index.html
7277 if path == '' :
7378 try :
7479 return send_from_directory (app .static_folder , 'index.html' )
7580 except Exception as e :
76- print (f"Error serving index.html: { e } " )
81+ logger . error (f"Error serving index.html: { e } " )
7782 return jsonify ({
7883 'error' : 'Frontend not built' ,
7984 'message' : 'Please build the frontend first with "npm run build"'
8085 }), 404
8186
8287 # Try to serve the requested file
8388 try :
84- print (f"Attempting to serve: { app .static_folder } /{ path } " )
89+ logger . debug (f"Attempting to serve: { app .static_folder } /{ path } " )
8590 return send_from_directory (app .static_folder , path )
8691 except Exception as e :
87- print (f"Error serving file: { e } " )
92+ logger . error (f"Error serving file: { e } " )
8893 # If file not found and not an asset, serve index.html for client-side routing
8994 # But if it's an asset or known file type, return 404
9095 if path .startswith ('assets/' ) or '.' in path .split ('/' )[- 1 ]:
@@ -129,10 +134,16 @@ def serve_static(path):
129134 parser = argparse .ArgumentParser (description = 'Run the Storage API backend server' )
130135 parser .add_argument ('-p' , '--port' , type = int , default = 5001 ,
131136 help = 'Port to run the server on (default: 5001)' )
137+ parser .add_argument ('-l' , '--log-level' , type = str , default = 'INFO' ,
138+ choices = ['DEBUG' , 'INFO' , 'WARNING' , 'ERROR' , 'CRITICAL' ],
139+ help = 'Set the logging level (default: INFO)' )
132140 args = parser .parse_args ()
133141
142+ # Set logging level based on argument
143+ logging .getLogger ().setLevel (getattr (logging , args .log_level ))
144+
134145 with app .app_context ():
135146 db .create_all ()
136147
137- print (f"Starting server on port { args .port } ..." )
148+ logger . info (f"Starting server on port { args .port } ..." )
138149 app .run (debug = True , port = args .port )
0 commit comments