Refactor: Enhance Flask Application Logging for Clarity and Conciseness
authorSam Mirazi <sasan345@gmail.com>
Tue, 3 Jun 2025 19:13:54 +0000 (12:13 -0700)
committerSam Mirazi <sasan345@gmail.com>
Tue, 3 Jun 2025 19:13:54 +0000 (12:13 -0700)
This commit refines the logging output of the Flask application (`app_flask/Flask_with_delay.py`) to improve readability and provide clearer status indicators during benchmark runs.

Two key changes were implemented:

1.  **Disable Werkzeug's Default Access Logs:**
    The standard Werkzeug request logger (which produces lines like `127.0.0.1 - - [timestamp] "GET / HTTP/1.1" 200 -`) has been disabled. This is achieved by getting the 'werkzeug' logger instance and setting its logging level to `ERROR`. This change significantly reduces log verbosity, allowing the custom application logs to be more prominent and easier to follow.

2.  **Add Explicit "OK" Confirmation to Custom Logs:**
    The custom log message printed by the Flask server upon successful request processing has been augmented with an " - OK" suffix. The log line now appears as `[Flask Server] Request {id} processed in {time}s - OK`. This provides an unambiguous visual confirmation that each request was handled successfully by the server, which is particularly useful when monitoring benchmark progress.

These modifications aim to make the Flask server's console output more focused and informative, aiding in the analysis of benchmark results by removing redundant log entries and clearly indicating successful operations.

app_flask/Flask_with_delay.py

index 904643e8beb775e94fdb8639313a25a78fbf1d67..b3afc75f6b4087f7137307eef8fd8a2b14019c9a 100644 (file)
@@ -2,9 +2,14 @@ from flask import Flask, Response
 import time
 import webbrowser
 from datetime import datetime # Added for timestamping
+import logging # Added import
 
 app = Flask(__name__)
 
+# Disable Werkzeug's default logger
+log = logging.getLogger('werkzeug')
+log.setLevel(logging.ERROR)
+
 request_counter = 0 # Global counter
 
 @app.route("/")
@@ -14,13 +19,13 @@ def home():
     request_counter = request_id
 
     start_time = datetime.now()
-    print(f"[Flask Server] Request {request_id} received at {start_time.strftime('%Y-%m-%d %H:%M:%S.%f')}")
+    # print(f"[Flask Server] Request {request_id} received at {start_time.strftime('%Y-%m-%d %H:%M:%S.%f')}") # Removed this line
     
     time.sleep(0.3)                # simulate slow work
     
     end_time = datetime.now()
     processing_time = (end_time - start_time).total_seconds()
-    print(f"[Flask Server] Request {request_id} finishing at {end_time.strftime('%Y-%m-%d %H:%M:%S.%f')}, processed in {processing_time:.2f}s")
+    print(f"[Flask Server] Request {request_id} processed in {processing_time:.2f}s - OK") # Kept and simplified this line, and added - OK
     
     html = f"<h1>Flask Server: Request {request_id} processed in {processing_time:.2f}s</h1>" # Updated content
     return Response(html, mimetype="text/html")