Skip to content

Commit

Permalink
feat: unified log formats of application and uvicorn (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
adubovik authored Dec 12, 2024
1 parent 01f8f42 commit 013d308
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
10 changes: 2 additions & 8 deletions aidial_analytics_realtime/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,15 @@
from aidial_analytics_realtime.time import parse_time
from aidial_analytics_realtime.topic_model import TopicModel
from aidial_analytics_realtime.universal_api_utils import merge
from aidial_analytics_realtime.utils.log_config import configure_loggers, logger

RATE_PATTERN = r"/v1/(.+?)/rate"
CHAT_COMPLETION_PATTERN = r"/openai/deployments/(.+?)/chat/completions"
EMBEDDING_PATTERN = r"/openai/deployments/(.+?)/embeddings"


app = FastAPI()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] - %(message)s",
handlers=[logging.StreamHandler()],
)

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
configure_loggers()


@app.on_event("startup")
Expand Down
39 changes: 39 additions & 0 deletions aidial_analytics_realtime/utils/log_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import logging
import sys

from uvicorn.logging import DefaultFormatter

logger = logging.getLogger("app")


def configure_loggers():
# Delegate uvicorn logs to the root logger
# to achieve uniform log formatting
for name, log in logging.getLogger().manager.loggerDict.items():
if isinstance(log, logging.Logger) and name.startswith("uvicorn"):
log.handlers = []
log.propagate = True

# Setting up log levels
logger.setLevel(logging.DEBUG)

# Configuring the root logger
root = logging.getLogger()
root.setLevel(logging.INFO)

root_has_stderr_handler = any(
isinstance(handler, logging.StreamHandler)
and handler.stream == sys.stderr
for handler in root.handlers
)

# Do not override the existing stderr handlers
# if they are already configured
if not root_has_stderr_handler:
formatter = DefaultFormatter(
fmt="%(asctime)s [%(levelname)s] - %(message)s"
)

handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(formatter)
root.addHandler(handler)

0 comments on commit 013d308

Please sign in to comment.