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

@@ -98,7 +98,7 @@ class MinerULoader:
files = {'files': (filename, f, 'application/octet-stream')}
log.info(f'Sending file to MinerU Local API: {filename}')
log.debug(f'Local API parameters: {form_data}')
log.debug('Local API parameters: %s', form_data)
response = requests.post(
f'{self.api_url}/file_parse',
@@ -233,7 +233,7 @@ class MinerULoader:
request_body['files'][0]['page_ranges'] = self.page_ranges
log.info(f'Requesting upload URL for: {filename}')
log.debug(f'Cloud API request body: {request_body}')
log.debug('Cloud API request body: %s', request_body)
try:
response = requests.post(
@@ -338,7 +338,7 @@ class MistralLoader:
headers = {**self.headers, 'Accept': 'application/json'}
async def url_request():
self._debug_log(f'Getting signed URL for file ID: {file_id}')
self._debug_log('Getting signed URL for file ID: %s', file_id)
async with session.get(
url,
headers=headers,
@@ -450,7 +450,7 @@ class MistralLoader:
try:
async def delete_request():
self._debug_log(f'Deleting file ID: {file_id}')
self._debug_log('Deleting file ID: %s', file_id)
async with session.delete(
url=f'{self.base_url}/files/{file_id}',
headers=self.headers,
@@ -460,7 +460,7 @@ class MistralLoader:
return await self._handle_response_async(response)
await self._retry_request_async(delete_request)
self._debug_log(f'File {file_id} deleted successfully')
self._debug_log('File %s deleted successfully', file_id)
except Exception as e:
# Don't fail the entire process if cleanup fails
@@ -519,7 +519,7 @@ class MistralLoader:
if page_content is None or page_index is None:
skipped_pages += 1
self._debug_log(
f"Skipping page due to missing 'markdown' or 'index'. Data keys: {list(page_data.keys())}"
"Skipping page due to missing 'markdown' or 'index'. Data keys: %s", list(page_data.keys())
)
continue
@@ -531,7 +531,7 @@ class MistralLoader:
if not cleaned_content:
skipped_pages += 1
self._debug_log(f'Skipping empty page {page_index}')
self._debug_log('Skipping empty page %s', page_index)
continue
# Create document with optimized metadata
@@ -90,7 +90,7 @@ class YoutubeLoader:
if self.proxy_url:
youtube_proxies = GenericProxyConfig(http_url=self.proxy_url, https_url=self.proxy_url)
log.debug(f'Using proxy URL: {self.proxy_url[:14]}...')
log.debug('Using proxy URL: %s...', self.proxy_url[:14])
else:
youtube_proxies = None
@@ -106,23 +106,23 @@ class YoutubeLoader:
try:
transcript = transcript_list.find_transcript([lang])
if transcript.is_generated:
log.debug(f"Found generated transcript for language '{lang}'")
log.debug("Found generated transcript for language '%s'", lang)
try:
transcript = transcript_list.find_manually_created_transcript([lang])
log.debug(f"Found manual transcript for language '{lang}'")
log.debug("Found manual transcript for language '%s'", lang)
except NoTranscriptFound:
log.debug(f"No manual transcript found for language '{lang}', using generated")
log.debug("No manual transcript found for language '%s', using generated", lang)
pass
log.debug(f"Found transcript for language '{lang}'")
log.debug("Found transcript for language '%s'", lang)
try:
transcript_pieces: List[Dict[str, Any]] = transcript.fetch()
except ParseError:
log.debug(f"Empty or invalid transcript for language '{lang}'")
log.debug("Empty or invalid transcript for language '%s'", lang)
continue
if not transcript_pieces:
log.debug(f"Empty transcript for language '{lang}'")
log.debug("Empty transcript for language '%s'", lang)
continue
transcript_text = ' '.join(
@@ -135,7 +135,7 @@ class YoutubeLoader:
)
return [Document(page_content=transcript_text, metadata=self._metadata)]
except NoTranscriptFound:
log.debug(f"No transcript found for language '{lang}'")
log.debug("No transcript found for language '%s'", lang)
continue
except Exception as e:
log.info(f"Error finding transcript for language '{lang}'")