Skip to content

Commit d219fe5

Browse files
committed
feat: Support Customizable Logging Format in Infinity
1 parent f98ccf4 commit d219fe5

File tree

3 files changed

+90
-8
lines changed

3 files changed

+90
-8
lines changed

libs/infinity_emb/infinity_emb/cli.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import re
66
import sys
77

8-
98
import infinity_emb
109
from infinity_emb._optional_imports import CHECK_TYPER, CHECK_UVICORN
1110
from infinity_emb.args import EngineArgs
@@ -107,6 +106,7 @@ def _construct(name: str):
107106

108107
tp = typer.Typer()
109108

109+
110110
@tp.command("v1")
111111
def v1(
112112
# v1 is deprecated. Please do no longer modify it.
@@ -177,6 +177,7 @@ def v1(
177177
proxy_root_path="", # set as empty string
178178
)
179179

180+
180181
@tp.command("v2")
181182
def v2(
182183
# t
@@ -380,6 +381,8 @@ def v2(
380381
api_key=api_key,
381382
proxy_root_path=proxy_root_path,
382383
)
384+
# Update logging configs
385+
set_uvicorn_logging_configs()
383386

384387
uvicorn.run(
385388
app,
@@ -391,6 +394,47 @@ def v2(
391394
)
392395

393396

397+
def set_uvicorn_logging_configs():
398+
"""Configure Uvicorn logging with environment variable overrides.
399+
400+
Allows customization of log formats through environment variables:
401+
- INFINITY_UVICORN_DEFAULT_FORMAT: Format for default logs
402+
- INFINITY_UVICORN_ACCESS_FORMAT: Format for access logs
403+
- INFINITY_UVICORN_DATE_FORMAT: Date format for all logs
404+
"""
405+
from uvicorn.config import LOGGING_CONFIG
406+
import os
407+
408+
# Define constants for environment variable names to improve maintainability
409+
default_format_env = MANAGER.uvicorn_default_format
410+
access_format_env = MANAGER.uvicorn_access_format
411+
date_format_env = MANAGER.uvicorn_date_format
412+
413+
# Default log format (can be overridden by env var)
414+
default_fmt = os.getenv(
415+
default_format_env,
416+
"%(asctime)s %(levelprefix)s %(message)s"
417+
)
418+
419+
# Access log format (can be overridden by env var)
420+
access_fmt = os.getenv(
421+
access_format_env,
422+
'%(asctime)s %(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s'
423+
)
424+
425+
# Date format for all logs (can be overridden by env var)
426+
date_fmt = os.getenv(
427+
date_format_env,
428+
"%Y-%m-%d %H:%M:%S"
429+
)
430+
431+
# Apply the configurations
432+
LOGGING_CONFIG["formatters"]["default"]["fmt"] = default_fmt
433+
LOGGING_CONFIG["formatters"]["default"]["datefmt"] = date_fmt
434+
LOGGING_CONFIG["formatters"]["access"]["fmt"] = access_fmt
435+
LOGGING_CONFIG["formatters"]["access"]["datefmt"] = date_fmt
436+
437+
394438
def cli():
395439
CHECK_TYPER.mark_required()
396440
if len(sys.argv) == 1 or sys.argv[1] not in [

libs/infinity_emb/infinity_emb/env.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,23 @@ def onnx_do_not_prefer_quantized(self):
271271
return self._to_bool_multiple(
272272
self._optional_infinity_var_multiple("onnx_do_not_prefer_quantized", default=["false"])
273273
)
274+
@cached_property
275+
def disable_rich_handler(self):
276+
return self._to_bool(self._optional_infinity_var("disable_rich_handler", default="false"))
277+
278+
@cached_property
279+
def log_format(self):
280+
return self._optional_infinity_var("log_format", default="%(asctime)s %(name)s %(levelname)s: %(message)s")
281+
@cached_property
282+
def uvicorn_default_format(self):
283+
return self._optional_infinity_var("uvicorn_default_format", default="[%(asctime)s] %(levelprefix)s %(message)s")
284+
285+
@cached_property
286+
def uvicorn_access_format(self):
287+
return self._optional_infinity_var("uvicorn_access_format", default='[%(asctime)s] %(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s')
288+
289+
@cached_property
290+
def uvicorn_date_format(self):
291+
return self._optional_infinity_var("uvicorn_date_format", default="%Y-%m-%d %H:%M:%S")
274292
MANAGER = __Infinity_EnvManager()
293+

libs/infinity_emb/infinity_emb/log_handler.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,39 @@
22
# Copyright (c) 2023-now michaelfeil
33

44
import logging
5+
import os
56
import sys
67
from enum import Enum
78
from typing import Any
89

10+
from infinity_emb.env import MANAGER
11+
912
logging.getLogger().handlers.clear()
1013

11-
handlers: list[Any] = []
12-
try:
13-
from rich.console import Console
14-
from rich.logging import RichHandler
14+
def configure_log_handlers() -> list[Any]:
15+
"""Configure and return logging handlers based on environment settings."""
16+
handlers = []
17+
18+
# Determine the appropriate handler
19+
if not MANAGER.disable_rich_handler:
20+
try:
21+
from rich.console import Console
22+
from rich.logging import RichHandler
23+
handlers.append(RichHandler(
24+
console=Console(stderr=True),
25+
show_time=False,
26+
markup=True
27+
))
28+
return handlers
29+
except ImportError:
30+
pass # Fall through to default handler
1531

16-
handlers.append(RichHandler(console=Console(stderr=True), show_time=False))
17-
except ImportError:
32+
# Default handler (used when Rich is disabled or not available)
1833
handlers.append(logging.StreamHandler(sys.stderr))
34+
return handlers
35+
36+
37+
handlers = configure_log_handlers()
1938

2039
LOG_LEVELS: dict[str, int] = {
2140
"critical": logging.CRITICAL,
@@ -26,7 +45,7 @@
2645
"trace": 5,
2746
}
2847

29-
FORMAT = "%(asctime)s %(name)s %(levelname)s: %(message)s"
48+
FORMAT = MANAGER.log_format
3049
logging.basicConfig(
3150
level="INFO",
3251
format=FORMAT,

0 commit comments

Comments
 (0)