Skip to content

Commit b60093c

Browse files
authored
Simple logger for backend log messages (#230)
1 parent b5a3271 commit b60093c

File tree

3 files changed

+192
-131
lines changed

3 files changed

+192
-131
lines changed

backend/restapi_server.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from flask import Flask, request, jsonify
99
from flasgger import Swagger
1010
from submodule.rs_device_manager import RsDeviceManager
11+
from submodule.rs_logger import log_setup, log, RsLogLevel
1112
from api.device import device_api
1213
from api.clock import clock_api
1314
from api.dsp import dsp_api
@@ -27,11 +28,17 @@ def main():
2728
parser.add_argument('device_file', type=str, help='Path to the input device xml file')
2829
parser.add_argument('--port', type=int, default=5000, help='Specify TCP Port to use for REST server')
2930
parser.add_argument('--debug', default=False, action='store_true', help='Enable/Disable debug mode')
31+
parser.add_argument('--logfile', type=str, default="rpe.log", help='Specify log file name')
32+
parser.add_argument('--maxbytes', type=int, default=2048, help='Specify maximun log file size in kilobytes before rollover')
33+
parser.add_argument('--backupcount', type=int, default=20, help='Specify no. of backup log files')
3034
args = parser.parse_args()
3135

36+
# setup app logger
37+
log_setup(filename=args.logfile, max_bytes=args.maxbytes*1024, backup_count=args.backupcount)
38+
3239
# Check if the device_file exists
3340
if os.path.exists(args.device_file) == False:
34-
print(f"ERROR: The file '{args.device_file}' does not exist.")
41+
log(f"Device file '{args.device_file}' does not exist.", RsLogLevel.ERROR)
3542
sys.exit(1)
3643

3744
# Parse Device XML file into Device List
@@ -64,6 +71,19 @@ def main():
6471
app.register_blueprint(attrs_api)
6572
app.register_blueprint(project_api)
6673

74+
# hook up request signal to log request by UI
75+
@app.before_request
76+
def before_request():
77+
log(f"{request.method} {request.url}")
78+
79+
@app.after_request
80+
def after_request(response):
81+
log(f"{request.method} {request.url} {response.status_code} - DONE")
82+
return response
83+
84+
# log app server started
85+
log("App server is running...")
86+
6787
# Start Rest API server
6888
app.run(debug=args.debug, port=args.port)
6989

0 commit comments

Comments
 (0)