Fix logging
This commit is contained in:
@ -2,17 +2,18 @@
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import code
|
import code
|
||||||
from collections.abc import Awaitable
|
|
||||||
import logging
|
import logging
|
||||||
|
from collections.abc import Awaitable
|
||||||
from typing import Any, cast
|
from typing import Any, cast
|
||||||
|
|
||||||
import click
|
import click
|
||||||
from sshecret_admin.services.admin_backend import AdminBackend
|
|
||||||
import uvicorn
|
import uvicorn
|
||||||
from pydantic import ValidationError
|
from pydantic import ValidationError
|
||||||
from sqlmodel import Session, create_engine, select
|
from sqlmodel import Session, create_engine, select
|
||||||
from sshecret_admin.auth.models import init_db, User, PasswordDB
|
|
||||||
from sshecret_admin.auth.authentication import hash_password
|
from sshecret_admin.auth.authentication import hash_password
|
||||||
|
from sshecret_admin.auth.models import PasswordDB, User, init_db
|
||||||
from sshecret_admin.core.settings import AdminServerSettings
|
from sshecret_admin.core.settings import AdminServerSettings
|
||||||
|
from sshecret_admin.services.admin_backend import AdminBackend
|
||||||
|
|
||||||
handler = logging.StreamHandler()
|
handler = logging.StreamHandler()
|
||||||
formatter = logging.Formatter(
|
formatter = logging.Formatter(
|
||||||
@ -20,6 +21,12 @@ formatter = logging.Formatter(
|
|||||||
)
|
)
|
||||||
handler.setFormatter(formatter)
|
handler.setFormatter(formatter)
|
||||||
|
|
||||||
|
LOG = logging.getLogger()
|
||||||
|
LOG.addHandler(handler)
|
||||||
|
|
||||||
|
LOG.setLevel(logging.INFO)
|
||||||
|
|
||||||
|
|
||||||
def create_user(session: Session, username: str, password: str) -> None:
|
def create_user(session: Session, username: str, password: str) -> None:
|
||||||
"""Create a user."""
|
"""Create a user."""
|
||||||
hashed_password = hash_password(password)
|
hashed_password = hash_password(password)
|
||||||
@ -33,20 +40,18 @@ def create_user(session: Session, username: str, password: str) -> None:
|
|||||||
@click.pass_context
|
@click.pass_context
|
||||||
def cli(ctx: click.Context, debug: bool) -> None:
|
def cli(ctx: click.Context, debug: bool) -> None:
|
||||||
"""Sshecret Admin."""
|
"""Sshecret Admin."""
|
||||||
LOG = logging.getLogger()
|
|
||||||
LOG.addHandler(handler)
|
|
||||||
|
|
||||||
if debug:
|
|
||||||
click.echo("Setting logging to debug level")
|
|
||||||
LOG.setLevel(logging.DEBUG)
|
|
||||||
else:
|
|
||||||
LOG.setLevel(logging.INFO)
|
|
||||||
try:
|
try:
|
||||||
settings = AdminServerSettings() # pyright: ignore[reportCallIssue]
|
settings = AdminServerSettings() # pyright: ignore[reportCallIssue]
|
||||||
except ValidationError as e:
|
except ValidationError as e:
|
||||||
raise click.ClickException(
|
raise click.ClickException(
|
||||||
"Error: One or more required environment options are missing."
|
"Error: One or more required environment options are missing."
|
||||||
) from e
|
) from e
|
||||||
|
|
||||||
|
if debug:
|
||||||
|
click.echo("Setting logging to debug level")
|
||||||
|
LOG.setLevel(logging.DEBUG)
|
||||||
|
settings.debug = True
|
||||||
ctx.obj = settings
|
ctx.obj = settings
|
||||||
|
|
||||||
|
|
||||||
@ -110,10 +115,22 @@ def cli_delete_user(ctx: click.Context, username: str) -> None:
|
|||||||
@click.option("--port", default=8822, type=click.INT)
|
@click.option("--port", default=8822, type=click.INT)
|
||||||
@click.option("--dev", is_flag=True)
|
@click.option("--dev", is_flag=True)
|
||||||
@click.option("--workers", type=click.INT)
|
@click.option("--workers", type=click.INT)
|
||||||
def cli_run(host: str, port: int, dev: bool, workers: int | None) -> None:
|
@click.pass_context
|
||||||
|
def cli_run(
|
||||||
|
ctx: click.Context, host: str, port: int, dev: bool, workers: int | None
|
||||||
|
) -> None:
|
||||||
"""Run the server."""
|
"""Run the server."""
|
||||||
|
settings = cast(AdminServerSettings, ctx.obj)
|
||||||
|
log_level = "info"
|
||||||
|
if settings.debug:
|
||||||
|
log_level = "debug"
|
||||||
uvicorn.run(
|
uvicorn.run(
|
||||||
"sshecret_admin.core.main:app", host=host, port=port, reload=dev, workers=workers
|
"sshecret_admin.core.main:app",
|
||||||
|
host=host,
|
||||||
|
port=port,
|
||||||
|
reload=dev,
|
||||||
|
workers=workers,
|
||||||
|
log_level=log_level,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user