Format Django initializing outputs using Khoj logger format

- Collect STDOUT from the `migrate', `collectstatic' commands and
  output using the Khoj logger format and verbosity settings

- Only show Django `collectstatic' command output in verbose mode

- Fix showing the Initializing Khoj log line by moving it after logger
  level set
This commit is contained in:
Debanjum Singh Solanky 2023-11-20 22:10:24 -08:00
parent 6d9091bef5
commit b06628ee31
2 changed files with 13 additions and 5 deletions

View file

@ -24,7 +24,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent.parent.parent
SECRET_KEY = os.getenv("KHOJ_DJANGO_SECRET_KEY") SECRET_KEY = os.getenv("KHOJ_DJANGO_SECRET_KEY")
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.getenv("KHOJ_DEBUG", "False") == "True" DEBUG = os.getenv("KHOJ_DEBUG") == "True"
ALLOWED_HOSTS = [".khoj.dev", "localhost", "127.0.0.1", "[::1]", "beta.khoj.dev"] ALLOWED_HOSTS = [".khoj.dev", "localhost", "127.0.0.1", "[::1]", "beta.khoj.dev"]

View file

@ -3,6 +3,8 @@
""" """
# Standard Packages # Standard Packages
from contextlib import redirect_stdout
import io
import os import os
import sys import sys
import locale import locale
@ -33,10 +35,14 @@ os.environ.setdefault("DJANGO_SETTINGS_MODULE", "khoj.app.settings")
django.setup() django.setup()
# Initialize Django Database # Initialize Django Database
call_command("migrate", "--noinput") db_migrate_output = io.StringIO()
with redirect_stdout(db_migrate_output):
call_command("migrate", "--noinput")
# Initialize Django Static Files # Initialize Django Static Files
call_command("collectstatic", "--noinput") collectstatic_output = io.StringIO()
with redirect_stdout(collectstatic_output):
call_command("collectstatic", "--noinput")
# Initialize the Application Server # Initialize the Application Server
app = FastAPI() app = FastAPI()
@ -79,14 +85,16 @@ def run(should_start_server=True):
args = cli(state.cli_args) args = cli(state.cli_args)
set_state(args) set_state(args)
logger.info(f"🚒 Initializing Khoj v{state.khoj_version}")
# Set Logging Level # Set Logging Level
if args.verbose == 0: if args.verbose == 0:
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)
elif args.verbose >= 1: elif args.verbose >= 1:
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
logger.info(f"🚒 Initializing Khoj v{state.khoj_version}")
logger.info(f"📦 Initializing DB:\n{db_migrate_output.getvalue().strip()}")
logger.debug(f"🌍 Initializing Web Client:\n{collectstatic_output.getvalue().strip()}")
initialization() initialization()
# Create app directory, if it doesn't exist # Create app directory, if it doesn't exist