rework quiet handler, use logging to log to file
This commit is contained in:
@@ -1,19 +1,36 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
from pyramid.config import Configurator
|
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
|
docport = 8000
|
||||||
|
|
||||||
|
|
||||||
class QuietHandler(WSGIRequestHandler):
|
class QuietHandler(WSGIRequestHandler):
|
||||||
# suppress “GET /…” access log
|
# suppress “GET /…” access log
|
||||||
def log_request(self, code="-", size="-"):
|
def log_request(self, code="-", size="-"):
|
||||||
|
logger.info("Request: {} {}".format(self.requestline, code))
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# suppress all other messages (errors, etc.)
|
# suppress all other messages (errors, etc.)
|
||||||
def log_message(self, fmt, *args):
|
def log_message(self, fmt, *args):
|
||||||
|
logger.error("Error: {}, Args: {}".format(fmt, args))
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def website():
|
def website():
|
||||||
config = Configurator()
|
config = Configurator()
|
||||||
|
|
||||||
@@ -24,25 +41,3 @@ def website():
|
|||||||
|
|
||||||
app = config.make_wsgi_app()
|
app = config.make_wsgi_app()
|
||||||
return 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
|
|
||||||
|
|||||||
Reference in New Issue
Block a user