Explorar o código

Add log level args for pc run (#182)

Alek Petuskey %!s(int64=2) %!d(string=hai) anos
pai
achega
af9733996a
Modificáronse 3 ficheiros con 31 adicións e 9 borrados
  1. 13 2
      pynecone/constants.py
  2. 3 1
      pynecone/pc.py
  3. 15 6
      pynecone/utils.py

+ 13 - 2
pynecone/constants.py

@@ -63,8 +63,8 @@ API_URL = "http://localhost:8000"
 BUN_PATH = "$HOME/.bun/bin/bun"
 # Command to install bun.
 INSTALL_BUN = "curl https://bun.sh/install | bash"
-# Command to run the backend in dev mode.
-RUN_BACKEND = "uvicorn --log-level debug --reload --host 0.0.0.0".split()
+# Default host in dev mode.
+BACKEND_HOST = "0.0.0.0"
 # The default timeout when launching the gunicorn server.
 TIMEOUT = 120
 # The command to run the backend in production mode.
@@ -135,6 +135,17 @@ class Env(str, Enum):
     PROD = "prod"
 
 
+# Log levels
+class LogLevel(str, Enum):
+    """The log levels."""
+
+    DEBUG = "debug"
+    INFO = "info"
+    WARNING = "warning"
+    ERROR = "error"
+    CRITICAL = "critical"
+
+
 class Endpoint(Enum):
     """Endpoints for the pynecone backend API."""
 

+ 3 - 1
pynecone/pc.py

@@ -52,6 +52,7 @@ def run(
     env: constants.Env = constants.Env.DEV,
     frontend: bool = True,
     backend: bool = True,
+    loglevel: constants.LogLevel = constants.LogLevel.ERROR,
 ):
     """Run the app.
 
@@ -59,6 +60,7 @@ def run(
         env: The environment to run the app in.
         frontend: Whether to run the frontend.
         backend: Whether to run the backend.
+        loglevel: The log level to use.
 
     Raises:
         Exit: If the app is not initialized.
@@ -93,7 +95,7 @@ def run(
     if frontend:
         frontend_cmd(app.app)
     if backend:
-        backend_cmd(app.__name__)
+        backend_cmd(app.__name__, loglevel=loglevel)
 
 
 @cli.command()

+ 15 - 6
pynecone/utils.py

@@ -13,6 +13,7 @@ import signal
 import string
 import subprocess
 import sys
+import uvicorn
 from collections import defaultdict
 from subprocess import PIPE
 from types import ModuleType
@@ -532,23 +533,29 @@ def get_num_workers() -> int:
     return (os.cpu_count() or 1) * 2 + 1
 
 
-def run_backend(app_name: str):
+def run_backend(app_name: str, loglevel: constants.LogLevel = constants.LogLevel.ERROR):
     """Run the backend.
 
     Args:
         app_name: The app name.
+        loglevel: The log level.
     """
-    command = constants.RUN_BACKEND + [
-        f"{app_name}:{constants.APP_VAR}.{constants.API_VAR}"
-    ]
-    subprocess.run(command)
+    uvicorn.run(
+        f"{app_name}:{constants.APP_VAR}.{constants.API_VAR}",
+        host=constants.BACKEND_HOST,
+        log_level=loglevel,
+        reload=True,
+    )
 
 
-def run_backend_prod(app_name: str):
+def run_backend_prod(
+    app_name: str, loglevel: constants.LogLevel = constants.LogLevel.ERROR
+):
     """Run the backend.
 
     Args:
         app_name: The app name.
+        loglevel: The log level.
     """
     num_workers = get_num_workers()
     command = constants.RUN_BACKEND_PROD + [
@@ -556,6 +563,8 @@ def run_backend_prod(app_name: str):
         str(num_workers),
         "--threads",
         str(num_workers),
+        "--log-level",
+        str(loglevel),
         f"{app_name}:{constants.APP_VAR}()",
     ]
     subprocess.run(command)