mirror of
https://github.com/dograh-hq/dograh.git
synced 2026-06-07 07:55:16 +02:00
chore: add interceptor in logging config (#55)
This commit is contained in:
parent
6efe7d6bd4
commit
451ba05c25
2 changed files with 42 additions and 2 deletions
|
|
@ -1,13 +1,15 @@
|
|||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
import loguru
|
||||
|
||||
from api.constants import ENVIRONMENT, SERIALIZE_LOG_OUTPUT
|
||||
from api.constants import SERIALIZE_LOG_OUTPUT
|
||||
from api.enums import Environment
|
||||
from api.utils.worker import get_worker_id, is_worker_process
|
||||
from pipecat.utils.context import run_id_var, turn_var
|
||||
|
||||
ENVIRONMENT = os.getenv("ENVIRONMENT", Environment.LOCAL.value)
|
||||
ENABLE_TURN_LOGGING = os.getenv("ENABLE_TURN_LOGGING", "false").lower() == "true"
|
||||
|
||||
# We write different uvicorn forked worker log to a different
|
||||
|
|
@ -18,6 +20,26 @@ LOG_FILE_PATH = os.getenv("LOG_FILE_PATH", None)
|
|||
_logging_initialized = False
|
||||
|
||||
|
||||
class InterceptHandler(logging.Handler):
|
||||
"""
|
||||
Intercept standard library logging calls and redirect them to loguru.
|
||||
This allows us to capture uvicorn and other library logs through loguru.
|
||||
"""
|
||||
|
||||
def emit(self, record):
|
||||
# Get corresponding Loguru level if it exists
|
||||
try:
|
||||
level = loguru.logger.level(record.levelname).name
|
||||
except ValueError:
|
||||
level = record.levelno
|
||||
|
||||
# Use the original record's information instead of trying to find the caller
|
||||
# This preserves the logger name (e.g., "uvicorn.access") in the logs
|
||||
loguru.logger.patch(lambda r: r.update(name=record.name)).opt(
|
||||
exception=record.exc_info
|
||||
).log(level, record.getMessage())
|
||||
|
||||
|
||||
def inject_run_id(record):
|
||||
"""Inject run_id from context variable into log record"""
|
||||
record["extra"]["run_id"] = run_id_var.get()
|
||||
|
|
@ -103,4 +125,16 @@ def setup_logging():
|
|||
)
|
||||
|
||||
loguru.logger = patched
|
||||
|
||||
# Intercept standard library logging (uvicorn, etc.) and redirect to loguru
|
||||
# Set level to INFO to avoid debug logs from libraries
|
||||
logging.basicConfig(handlers=[InterceptHandler()], level=logging.INFO, force=True)
|
||||
|
||||
# Specifically intercept uvicorn loggers with INFO level
|
||||
for logger_name in ["uvicorn", "uvicorn.error", "uvicorn.access"]:
|
||||
logging_logger = logging.getLogger(logger_name)
|
||||
logging_logger.handlers = [InterceptHandler()]
|
||||
logging_logger.setLevel(logging.INFO)
|
||||
logging_logger.propagate = False
|
||||
|
||||
_logging_initialized = True
|
||||
|
|
|
|||
|
|
@ -65,7 +65,13 @@
|
|||
let apiBaseUrl = DEFAULT_CONFIG.apiBaseUrl;
|
||||
if (apiEndpoint) {
|
||||
// Use the apiEndpoint from URL parameter if provided
|
||||
apiBaseUrl = apiEndpoint.replace(/\/+$/, ''); // Remove trailing slashes
|
||||
// Ensure it has a protocol
|
||||
if (!apiEndpoint.startsWith('http://') && !apiEndpoint.startsWith('https://')) {
|
||||
// Default to https for production endpoints
|
||||
apiBaseUrl = 'https://' + apiEndpoint.replace(/\/+$/, '');
|
||||
} else {
|
||||
apiBaseUrl = apiEndpoint.replace(/\/+$/, ''); // Remove trailing slashes
|
||||
}
|
||||
} else if (scriptUrl.origin.includes('localhost')) {
|
||||
apiBaseUrl = 'http://localhost:8000';
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue