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
2026-08-02 15:39:10 -05:00
committed by GitHub
parent 52145eede9
commit 2d18727ab8
48 changed files with 297 additions and 247 deletions
+5 -5
View File
@@ -331,7 +331,7 @@ async def disconnect_user_sessions(user_id: str):
for sid in session_ids:
await sio.disconnect(sid)
if session_ids:
log.info(f'Disconnected {len(session_ids)} session(s) for user {user_id}')
log.info('Disconnected %s session(s) for user %s', len(session_ids), user_id)
except Exception as e:
log.warning(f'Failed to disconnect sessions for user {user_id}: {e}')
@@ -626,7 +626,7 @@ async def ydoc_document_join(sid, data):
user_name = data.get('user_name', 'Anonymous')
user_color = data.get('user_color', '#000000')
log.info(f'User {user_id} joining document {document_id}')
log.info('User %s joining document %s', user_id, document_id)
await YDOC_MANAGER.add_user(document_id=document_id, user_id=sid)
# Join Socket.IO room
@@ -665,7 +665,7 @@ async def ydoc_document_join(sid, data):
skip_sid=sid,
)
log.info(f'User {user_id} successfully joined document {document_id}')
log.info('User %s successfully joined document %s', user_id, document_id)
except Exception as e:
log.error(f'Error in yjs_document_join: {e}')
@@ -826,7 +826,7 @@ async def yjs_document_leave(sid, data):
try:
document_id = normalize_document_id(data['document_id'])
log.info(f'User {user["id"]} leaving document {document_id}')
log.info('User %s leaving document %s', user['id'], document_id)
# Remove user from the document
await YDOC_MANAGER.remove_user(document_id=document_id, user_id=sid)
@@ -842,7 +842,7 @@ async def yjs_document_leave(sid, data):
)
if await YDOC_MANAGER.document_exists(document_id) and len(await YDOC_MANAGER.get_users(document_id)) == 0:
log.info(f'Cleaning up document {document_id} as no users are left')
log.info('Cleaning up document %s as no users are left', document_id)
await YDOC_MANAGER.clear_document(document_id)
except Exception as e: