Skip to content

Commit c1eb55b

Browse files
committed
refactor(logging): use centralized logging helpers in consumers
Replace inline log-level resolution and handler creation in lightspeed_stack.py and runners/uvicorn.py with calls to the shared resolve_log_level() and create_log_handler() from log.py. Remove the private _resolve_log_level() from uvicorn.py and the direct RichHandler import from lightspeed_stack.py. Signed-off-by: Major Hayden <major@redhat.com>
1 parent 9767428 commit c1eb55b

2 files changed

Lines changed: 12 additions & 46 deletions

File tree

src/lightspeed_stack.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,31 @@
99
import sys
1010
from argparse import ArgumentParser
1111

12-
13-
from rich.logging import RichHandler
14-
15-
from log import get_logger
12+
from log import get_logger, resolve_log_level, create_log_handler
1613
from configuration import configuration
1714
from runners.uvicorn import start_uvicorn
1815
from runners.quota_scheduler import start_quota_scheduler
1916
from utils import schema_dumper
20-
from constants import LIGHTSPEED_STACK_LOG_LEVEL_ENV_VAR, DEFAULT_LOG_LEVEL
21-
22-
# Read log level from environment variable with validation
23-
log_level_str = os.environ.get(LIGHTSPEED_STACK_LOG_LEVEL_ENV_VAR, DEFAULT_LOG_LEVEL)
24-
log_level = getattr(logging, log_level_str.upper(), None)
25-
if not isinstance(log_level, int):
26-
print(
27-
f"WARNING: Invalid log level '{log_level_str}', falling back to {DEFAULT_LOG_LEVEL}",
28-
file=sys.stderr,
29-
)
30-
log_level = getattr(logging, DEFAULT_LOG_LEVEL)
17+
from constants import LIGHTSPEED_STACK_LOG_LEVEL_ENV_VAR
18+
19+
# Resolve log level and handler from centralized logging utilities
20+
log_level = resolve_log_level()
3121

32-
# RichHandler's columnar layout produces very narrow log output in containers
33-
# without a TTY (Rich falls back to 80 columns, columns consume ~40 of those).
34-
# Use a plain format when there's no terminal attached.
22+
# Configure root logger. basicConfig(force=True) is intentionally root-logger-specific.
23+
# RichHandler needs format="%(message)s" to prevent double-formatting by the root Formatter.
24+
handler = create_log_handler()
3525
if sys.stderr.isatty():
3626
logging.basicConfig(
3727
level=log_level,
3828
format="%(message)s",
3929
datefmt="[%X]",
40-
handlers=[RichHandler()],
30+
handlers=[handler],
4131
force=True,
4232
)
4333
else:
4434
logging.basicConfig(
4535
level=log_level,
46-
format="%(asctime)s %(levelname)-8s %(name)s:%(lineno)d %(message)s",
36+
handlers=[handler],
4737
force=True,
4838
)
4939

src/runners/uvicorn.py

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,15 @@
11
"""Uvicorn runner."""
22

33
import logging
4-
import os
54

65
import uvicorn
76

8-
from constants import DEFAULT_LOG_LEVEL, LIGHTSPEED_STACK_LOG_LEVEL_ENV_VAR
9-
from log import get_logger
7+
from log import get_logger, resolve_log_level
108
from models.config import ServiceConfiguration
119

1210
logger = get_logger(__name__)
1311

1412

15-
def _resolve_log_level() -> int:
16-
"""Resolve the uvicorn log level from the environment.
17-
18-
Reads the LIGHTSPEED_STACK_LOG_LEVEL environment variable and converts it
19-
to a Python logging level constant. Falls back to the default log level
20-
when the variable is unset or contains an invalid value.
21-
22-
Returns:
23-
The resolved logging level as an integer constant.
24-
"""
25-
level_str = os.environ.get(LIGHTSPEED_STACK_LOG_LEVEL_ENV_VAR, DEFAULT_LOG_LEVEL)
26-
level = getattr(logging, level_str.upper(), None)
27-
if not isinstance(level, int):
28-
logger.warning(
29-
"Invalid log level '%s', falling back to %s",
30-
level_str,
31-
DEFAULT_LOG_LEVEL,
32-
)
33-
level = getattr(logging, DEFAULT_LOG_LEVEL)
34-
return level
35-
36-
3713
def start_uvicorn(configuration: ServiceConfiguration) -> None:
3814
"""Start the Uvicorn server using the provided service configuration.
3915
@@ -43,7 +19,7 @@ def start_uvicorn(configuration: ServiceConfiguration) -> None:
4319
`tls_certificate_path`, and `tls_key_password`). TLS fields may be None
4420
and will be forwarded to uvicorn.run as provided.
4521
"""
46-
log_level = _resolve_log_level()
22+
log_level = resolve_log_level()
4723
logger.info("Starting Uvicorn with log level %s", logging.getLevelName(log_level))
4824

4925
# please note:

0 commit comments

Comments
 (0)