Suppress debug logs from uvicorn.error to avoid clutter from websockets

- If application is not in DEBUG_MODE
This commit is contained in:
sabaimran 2024-04-12 12:12:06 +05:30
parent 89b8ec3546
commit 1377a44a1a

View file

@ -2,14 +2,14 @@
isort:skip_file isort:skip_file
""" """
from contextlib import redirect_stdout from contextlib import redirect_stdout
import logging
import io import io
import os import os
import sys import sys
import locale import locale
import logging from rich.logging import RichHandler
import threading import threading
import warnings import warnings
from importlib.metadata import version from importlib.metadata import version
@ -26,7 +26,6 @@ import django
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from rich.logging import RichHandler
import schedule import schedule
from django.core.asgi import get_asgi_application from django.core.asgi import get_asgi_application
@ -36,6 +35,15 @@ from django.core.management import call_command
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "khoj.app.settings") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "khoj.app.settings")
django.setup() django.setup()
# Setup Logger
rich_handler = RichHandler(rich_tracebacks=True)
rich_handler.setFormatter(fmt=logging.Formatter(fmt="%(name)s: %(message)s", datefmt="[%H:%M:%S.%f]"))
logging.basicConfig(handlers=[rich_handler])
logging.getLogger("uvicorn.error").setLevel(logging.INFO)
logger = logging.getLogger("khoj")
# Initialize Django Database # Initialize Django Database
db_migrate_output = io.StringIO() db_migrate_output = io.StringIO()
with redirect_stdout(db_migrate_output): with redirect_stdout(db_migrate_output):
@ -82,13 +90,6 @@ from khoj.utils import state
from khoj.utils.cli import cli from khoj.utils.cli import cli
from khoj.utils.initialization import initialization from khoj.utils.initialization import initialization
# Setup Logger
rich_handler = RichHandler(rich_tracebacks=True)
rich_handler.setFormatter(fmt=logging.Formatter(fmt="%(message)s", datefmt="[%H:%M:%S.%f]"))
logging.basicConfig(handlers=[rich_handler])
logger = logging.getLogger("khoj")
def run(should_start_server=True): def run(should_start_server=True):
# Turn Tokenizers Parallelism Off. App does not support it. # Turn Tokenizers Parallelism Off. App does not support it.
@ -161,7 +162,13 @@ def start_server(app, host=None, port=None, socket=None):
uvicorn.run(app, proxy_headers=True, uds=socket, log_level="debug", use_colors=True, log_config=None) uvicorn.run(app, proxy_headers=True, uds=socket, log_level="debug", use_colors=True, log_config=None)
else: else:
uvicorn.run( uvicorn.run(
app, host=host, port=port, log_level="debug", use_colors=True, log_config=None, timeout_keep_alive=60 app,
host=host,
port=port,
log_level="debug" if in_debug_mode() else "info",
use_colors=True,
log_config=None,
timeout_keep_alive=60,
) )
logger.info("🌒 Stopping Khoj") logger.info("🌒 Stopping Khoj")