perf: build debug log messages lazily so disabled debug logs cost nothing (#27834)

GLOBAL_LOG_LEVEL defaults to INFO, so every log.debug(...) in the backend is discarded, but the message is built first: 187 call sites interpolate their payload into an f-string before the logging call runs, so the work happens on every request and the result is thrown away. The worst one sits in process_chat_payload and stringifies the whole request body, full conversation history included, once per chat completion.

That one line with DEBUG disabled, CPython 3.12:

| conversation | payload | before   | after   |
| ------------ | ------- | -------- | ------- |
| 4 messages   | 1.2 kB  | 3.4 us   | 0.07 us |
| 20 messages  | 17 kB   | 24.8 us  | 0.07 us |
| 60 messages  | 123 kB  | 216.6 us | 0.07 us |

The lazy form log.debug('form_data: %s', form_data) hands the payload to record.getMessage(), which the InterceptHandler only reaches once a record has passed the level check. With DEBUG enabled the emitted lines are byte-identical, f'{x=}' sites included: those map to %r. MistralLoader._debug_log callers get the same treatment, since that wrapper already forwards *args.
This commit is contained in:
Classic298 authored and GitHub committed 2026-07-31 19:09:01 -05:00
1 parent b67804f2b6
commit 52cfb02c72
40 files changed
+198 -190

No files matched your search

+4 -4
View File
@@ -230,7 +230,7 @@ def get_rf(
eos = getattr(cfg, 'eos_token_id', None)
if eos is not None:
cfg.pad_token_id = eos
log.debug(f'Missing pad_token_id detected; set to eos_token_id={eos}')
log.debug('Missing pad_token_id detected; set to eos_token_id=%s', eos)
else:
log.warning('Neither pad_token_id nor eos_token_id present in model config')
except Exception as e2:
@@ -1643,7 +1643,7 @@ def save_docs_to_vector_db(
return ', '.join(docs_info)
log.debug(f'save_docs_to_vector_db: document {_get_docs_info(docs)} {collection_name}')
log.debug('save_docs_to_vector_db: document %s %s', _get_docs_info(docs), collection_name)
# Check if entries with the same hash (metadata.hash) already exist
if metadata and 'hash' in metadata:
@@ -2567,7 +2567,7 @@ async def process_web_search(request: Request, form_data: SearchForm, user=Depen
result_items = []
try:
logging.debug(f'trying to web search with {config.WEB_SEARCH_ENGINE, form_data.queries}')
logging.debug('trying to web search with %s', (config.WEB_SEARCH_ENGINE, form_data.queries))
# Use semaphore to limit concurrent requests based on WEB_SEARCH_CONCURRENT_REQUESTS
# 0 or None = unlimited (previous behavior), positive number = limited concurrency
@@ -2610,7 +2610,7 @@ async def process_web_search(request: Request, form_data: SearchForm, user=Depen
urls.append(item.link)
urls = list(dict.fromkeys(urls))
log.debug(f'urls: {urls}')
log.debug('urls: %s', urls)
except Exception as e:
log.exception('Web search failed')