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

+14 -14
View File
@@ -548,8 +548,8 @@ async def ldap_auth(
]
if ENABLE_LDAP_GROUP_MANAGEMENT:
search_attributes.append(f'{LDAP_ATTRIBUTE_FOR_GROUPS}')
log.info(f'LDAP Group Management enabled. Adding {LDAP_ATTRIBUTE_FOR_GROUPS} to search attributes')
log.info(f'LDAP search attributes: {search_attributes}')
log.info('LDAP Group Management enabled. Adding %s to search attributes', LDAP_ATTRIBUTE_FOR_GROUPS)
log.info('LDAP search attributes: %s', search_attributes)
search_success = await asyncio.to_thread(
connection_app.search,
@@ -586,30 +586,30 @@ async def ldap_auth(
user_groups = []
if ENABLE_LDAP_GROUP_MANAGEMENT and LDAP_ATTRIBUTE_FOR_GROUPS in entry:
group_dns = entry[LDAP_ATTRIBUTE_FOR_GROUPS]
log.info(f'LDAP raw group DNs for user {username_list}: {group_dns}')
log.info('LDAP raw group DNs for user %s: %s', username_list, group_dns)
if group_dns:
log.info(f'LDAP group_dns original: {group_dns}')
log.info(f'LDAP group_dns type: {type(group_dns)}')
log.info(f'LDAP group_dns length: {len(group_dns)}')
log.info('LDAP group_dns original: %s', group_dns)
log.info('LDAP group_dns type: %s', type(group_dns))
log.info('LDAP group_dns length: %s', len(group_dns))
if hasattr(group_dns, 'value'):
group_dns = group_dns.value
log.info(f'Extracted .value property: {group_dns}')
log.info('Extracted .value property: %s', group_dns)
elif hasattr(group_dns, '__iter__') and not isinstance(group_dns, (str, bytes)):
group_dns = list(group_dns)
log.info(f'Converted to list: {group_dns}')
log.info('Converted to list: %s', group_dns)
if isinstance(group_dns, list):
group_dns = [str(item) for item in group_dns]
else:
group_dns = [str(group_dns)]
log.info(f'LDAP group_dns after processing - type: {type(group_dns)}, length: {len(group_dns)}')
log.info('LDAP group_dns after processing - type: %s, length: %s', type(group_dns), len(group_dns))
for group_idx, group_dn in enumerate(group_dns):
group_dn = str(group_dn)
log.info(f'Processing group DN #{group_idx + 1}: {group_dn}')
log.info('Processing group DN #%s: %s', group_idx + 1, group_dn)
try:
group_cn = extract_group_cn_from_dn(group_dn)
@@ -621,9 +621,9 @@ async def ldap_auth(
except Exception as e:
log.warning(f'Failed to extract group name from DN {group_dn}: {e}')
log.info(f'LDAP groups for user {username_list}: {user_groups} (total: {len(user_groups)})')
log.info('LDAP groups for user %s: %s (total: %s)', username_list, user_groups, len(user_groups))
else:
log.info(f'No groups found for user {username_list}')
log.info('No groups found for user %s', username_list)
elif ENABLE_LDAP_GROUP_MANAGEMENT:
log.warning(
f'LDAP Group Management enabled but {LDAP_ATTRIBUTE_FOR_GROUPS} attribute not found in user entry'
@@ -691,7 +691,7 @@ async def ldap_auth(
if ENABLE_LDAP_GROUP_CREATION:
await Groups.create_groups_by_group_names(user.id, user_groups, db=db)
await Groups.sync_groups_by_group_names(user.id, user_groups, db=db)
log.info(f'Successfully synced groups for user {user.id}: {user_groups}')
log.info('Successfully synced groups for user %s: %s', user.id, user_groups)
except Exception as e:
log.error(f'Failed to sync groups for user {user.id}: {e}')
@@ -1156,7 +1156,7 @@ async def get_admin_details(
admin_email = await Config.get('auth.admin.email')
admin_name = None
log.info(f'Admin details - Email: {admin_email}, Name: {admin_name}')
log.info('Admin details - Email: %s, Name: %s', admin_email, admin_name)
if admin_email:
admin = await Users.get_user_by_email(admin_email, db=db)