perf: build info log messages lazily so raising the log level actually saves work (#27837)

Raising GLOBAL_LOG_LEVEL to WARNING buys quieter output but not less work: 241 INFO call sites interpolate their payload into an f-string before the logging call gets to drop it. The heaviest is get_doc, which logs every chunk id and metadata dict in a collection, so on the full-context retrieval path that is the entire knowledge base, once per chat request.

That one line at WARNING, CPython 3.12:

| knowledge base | payload | before   | after   |
| -------------- | ------- | -------- | ------- |
| top-k of 3     | 1.2 kB  | 3.8 us   | 0.07 us |
| 500 chunks     | 201 kB  | 583.6 us | 0.08 us |
| 5000 chunks    | 2.0 MB  | 5.8 ms   | 0.15 us |

The lazy form log.info('query_doc:result %s %s', result.ids, result.metadatas) hands the payload to record.getMessage(), which the InterceptHandler only reaches once a record has passed the level check. Output at INFO is byte-identical. Two sites that already built their message eagerly, one str concat and one % operator, move to the same lazy form.
This commit is contained in:
Classic298 authored and GitHub committed 2026-08-02 15:39:10 -05:00
1 parent 52145eede9
commit 2d18727ab8
48 files changed
+297 -247

No files matched your search

+8 -6
View File
@@ -157,7 +157,7 @@ def convert_audio_to_mp3(file_path):
output_path = os.path.splitext(file_path)[0] + '.mp3'
audio = AudioSegment.from_file(file_path)
audio.export(output_path, format='mp3')
log.info(f'Converted {file_path} to {output_path}')
log.info('Converted %s to %s', file_path, output_path)
return output_path
except Exception as e:
log.error(f'Error converting audio file: {e}')
@@ -208,7 +208,7 @@ def transcode_audio_to_mp3(audio_data: bytes, content_type_header: str, output_p
audio_segment = AudioSegment.from_file(io.BytesIO(audio_data))
audio_segment.export(str(output_path), format='mp3')
log.info(f'Transcoded {mime_type} audio to MP3: {output_path}')
log.info('Transcoded %s audio to MP3: %s', mime_type, output_path)
return True
@@ -631,7 +631,7 @@ async def _transcribe_whisper(request, file_path, languages, file_dir, id):
language=languages[0],
multilingual=WHISPER_MULTILINGUAL,
)
log.info("Detected language '%s' with probability %f" % (info.language, info.language_probability))
log.info("Detected language '%s' with probability %f", info.language, info.language_probability)
return ''.join([segment.text for segment in list(segments)])
transcript = await asyncio.to_thread(_run)
@@ -952,7 +952,9 @@ async def _transcribe_mistral(request, file_path, filename, metadata, file_dir,
try:
model = await Config.get('audio.stt.model') or 'voxtral-mini-latest'
log.info(
f'Mistral STT - model: {model}, method: {"chat_completions" if use_chat_completions else "transcriptions"}'
'Mistral STT - model: %s, method: %s',
model,
'chat_completions' if use_chat_completions else 'transcriptions',
)
session = await get_session()
@@ -1075,7 +1077,7 @@ async def _transcribe_mistral(request, file_path, filename, metadata, file_dir,
async def transcribe(request: Request, file_path: str, metadata: Optional[dict] = None, user=None):
log.info(f'transcribe: {file_path} {metadata}')
log.info('transcribe: %s %s', file_path, metadata)
if BYPASS_PYDUB_PREPROCESSING:
log.info('Bypassing pydub preprocessing (BYPASS_PYDUB_PREPROCESSING=true)')
@@ -1202,7 +1204,7 @@ async def transcription(
status_code=status.HTTP_403_FORBIDDEN,
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
)
log.info(f'file.content_type: {file.content_type}')
log.info('file.content_type: %s', file.content_type)
stt_supported_content_types = await Config.get('audio.stt.supported_content_types', [])
if not strict_match_mime_type(stt_supported_content_types, file.content_type):