diff --git a/src/utils/documentation.py b/src/utils/documentation.py index 1dc00c5..88789e5 100644 --- a/src/utils/documentation.py +++ b/src/utils/documentation.py @@ -1,19 +1,36 @@ import os -import sys from pyramid.config import Configurator -from wsgiref.simple_server import make_server, WSGIRequestHandler +from wsgiref.simple_server import WSGIRequestHandler +from src import LOG_DIR +import logging + + +log_path = os.path.join(LOG_DIR, "web_documentation.log") + +# Replace the default StreamHandler with a FileHandler +logging.basicConfig( + level=logging.INFO, + handlers=[logging.FileHandler(log_path, encoding="utf-8")], + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", +) + +logger = logging.getLogger(__name__) # inherits the same file handler docport = 8000 + class QuietHandler(WSGIRequestHandler): # suppress “GET /…” access log def log_request(self, code="-", size="-"): + logger.info("Request: {} {}".format(self.requestline, code)) pass # suppress all other messages (errors, etc.) def log_message(self, fmt, *args): + logger.error("Error: {}, Args: {}".format(fmt, args)) pass + def website(): config = Configurator() @@ -24,25 +41,3 @@ def website(): app = config.make_wsgi_app() return app - - -def launch_documentation(): - app = website() - server = make_server("localhost", docport, app, handler_class=QuietHandler) - print("Serving MkDocs documentation on http://0.0.0.0:{}".format(docport)) - server.serve_forever() - - -def run_mkdocs(): - with open(os.devnull, "w") as devnull: - old_stdout = sys.stdout - old_stderr = sys.stderr - sys.stdout = devnull - sys.stderr = devnull - try: - launch_documentation() - except Exception as e: - print("Error occurred while launching documentation:", e) - finally: - sys.stdout = old_stdout - sys.stderr = old_stderr