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:
@@ -542,7 +542,7 @@ async def create_admin_user(email: str, password: str, name: str = 'Admin'):
|
||||
log.debug('Users already exist, skipping admin creation')
|
||||
return None
|
||||
|
||||
log.info(f'Creating admin account from environment variables: {email}')
|
||||
log.info('Creating admin account from environment variables: %s', email)
|
||||
try:
|
||||
hashed = await get_password_hash(password)
|
||||
user = await Auths.insert_new_auth(
|
||||
@@ -552,7 +552,7 @@ async def create_admin_user(email: str, password: str, name: str = 'Admin'):
|
||||
role='admin',
|
||||
)
|
||||
if user:
|
||||
log.info(f'Admin account created successfully: {email}')
|
||||
log.info('Admin account created successfully: %s', email)
|
||||
return user
|
||||
else:
|
||||
log.error('Failed to create admin account from environment variables')
|
||||
|
||||
@@ -211,8 +211,9 @@ async def scheduler_worker_loop(app) -> None:
|
||||
SCHEDULER_POLL_INTERVAL env var (default: 10 seconds).
|
||||
"""
|
||||
log.info(
|
||||
f'Scheduler worker started (timer poll interval: {TIMER_POLL_INTERVAL}s, '
|
||||
f'scheduler poll interval: {SCHEDULER_POLL_INTERVAL}s)'
|
||||
'Scheduler worker started (timer poll interval: %ss, scheduler poll interval: %ss)',
|
||||
TIMER_POLL_INTERVAL,
|
||||
SCHEDULER_POLL_INTERVAL,
|
||||
)
|
||||
next_scheduler_poll = 0.0
|
||||
|
||||
@@ -240,7 +241,7 @@ async def scheduler_worker_loop(app) -> None:
|
||||
async with get_async_db() as db:
|
||||
batch = await Automations.claim_due(int(time.time_ns()), limit=10, db=db)
|
||||
if batch:
|
||||
log.info(f'Claimed {len(batch)} due automation(s)')
|
||||
log.info('Claimed %s due automation(s)', len(batch))
|
||||
for automation in batch:
|
||||
asyncio.create_task(execute_automation(app, automation))
|
||||
except Exception:
|
||||
|
||||
@@ -68,7 +68,7 @@ async def generate_direct_chat_completion(
|
||||
)
|
||||
|
||||
channel = f'{user_id}:{session_id}:{request_id}'
|
||||
logging.info(f'WebSocket channel: {channel}')
|
||||
logging.info('WebSocket channel: %s', channel)
|
||||
|
||||
if form_data.get('stream'):
|
||||
q = asyncio.Queue()
|
||||
@@ -95,7 +95,7 @@ async def generate_direct_chat_completion(
|
||||
}
|
||||
)
|
||||
|
||||
log.info(f'res: {res}')
|
||||
log.info('res: %s', res)
|
||||
|
||||
if res.get('status', False):
|
||||
# Define a generator to stream responses
|
||||
|
||||
@@ -1193,7 +1193,7 @@ def stream_chunks_handler(stream: aiohttp.StreamReader):
|
||||
if len(line) > max_buffer_size:
|
||||
skip_mode = True
|
||||
yield b'data: {}\n'
|
||||
log.info(f'Skip mode triggered, line size: {len(line)}')
|
||||
log.info('Skip mode triggered, line size: %s', len(line))
|
||||
else:
|
||||
yield line + b'\n'
|
||||
|
||||
@@ -1203,7 +1203,7 @@ def stream_chunks_handler(stream: aiohttp.StreamReader):
|
||||
# Check if buffer exceeds limit
|
||||
if not skip_mode and len(buffer) > max_buffer_size:
|
||||
skip_mode = True
|
||||
log.info(f'Skip mode triggered, buffer size: {len(buffer)}')
|
||||
log.info('Skip mode triggered, buffer size: %s', len(buffer))
|
||||
# Clear oversized buffer to prevent unlimited growth
|
||||
buffer = b''
|
||||
|
||||
|
||||
@@ -377,13 +377,13 @@ async def get_all_models(request, refresh: bool = False, user: UserModel = None)
|
||||
if items is None:
|
||||
action_function = functions_by_id.get(action_id)
|
||||
if action_function is None:
|
||||
log.info(f'Action not found: {action_id}')
|
||||
log.info('Action not found: %s', action_id)
|
||||
action_items_by_id[action_id] = []
|
||||
continue
|
||||
|
||||
function_module = functions_cache.get(action_id)
|
||||
if function_module is None:
|
||||
log.info(f'Failed to load action module: {action_id}')
|
||||
log.info('Failed to load action module: %s', action_id)
|
||||
action_items_by_id[action_id] = []
|
||||
continue
|
||||
items = get_action_items_from_module(action_function, function_module)
|
||||
@@ -397,13 +397,13 @@ async def get_all_models(request, refresh: bool = False, user: UserModel = None)
|
||||
if items is None:
|
||||
filter_function = functions_by_id.get(filter_id)
|
||||
if filter_function is None:
|
||||
log.info(f'Filter not found: {filter_id}')
|
||||
log.info('Filter not found: %s', filter_id)
|
||||
filter_items_by_id[filter_id] = []
|
||||
continue
|
||||
|
||||
function_module = functions_cache.get(filter_id)
|
||||
if function_module is None:
|
||||
log.info(f'Failed to load filter module: {filter_id}')
|
||||
log.info('Failed to load filter module: %s', filter_id)
|
||||
filter_items_by_id[filter_id] = []
|
||||
continue
|
||||
if getattr(function_module, 'toggle', None):
|
||||
|
||||
@@ -608,7 +608,9 @@ async def get_oauth_client_info_with_dynamic_client_registration(
|
||||
}
|
||||
)
|
||||
log.info(
|
||||
f'Dynamic client registration successful at {registration_url}, client_id: {oauth_client_info.client_id}'
|
||||
'Dynamic client registration successful at %s, client_id: %s',
|
||||
registration_url,
|
||||
oauth_client_info.client_id,
|
||||
)
|
||||
return oauth_client_info
|
||||
except Exception as e:
|
||||
@@ -701,7 +703,7 @@ async def get_oauth_client_info_with_static_credentials(
|
||||
)
|
||||
|
||||
log.info(
|
||||
f'Static OAuth client info built for {oauth_client_id} using metadata from {oauth_server_metadata_url}'
|
||||
'Static OAuth client info built for %s using metadata from %s', oauth_client_id, oauth_server_metadata_url
|
||||
)
|
||||
return oauth_client_info
|
||||
except Exception as e:
|
||||
@@ -807,7 +809,7 @@ async def recover_static_oauth_client_metadata(connection: dict, oauth_client_in
|
||||
recovered = {**oauth_client_info}
|
||||
if not recovered.get('scope') and resource_metadata.scopes_supported:
|
||||
recovered['scope'] = ' '.join(resource_metadata.scopes_supported)
|
||||
log.info(f'Recovered static OAuth scopes for {server_url} from protected resource metadata')
|
||||
log.info('Recovered static OAuth scopes for %s from protected resource metadata', server_url)
|
||||
|
||||
if not recovered.get('resource') and resource_metadata.resource:
|
||||
recovered['resource'] = resource_metadata.resource
|
||||
@@ -916,7 +918,7 @@ class OAuthClientManager:
|
||||
def remove_client(self, client_id):
|
||||
if client_id in self.clients:
|
||||
del self.clients[client_id]
|
||||
log.info(f'Removed OAuth client {client_id}')
|
||||
log.info('Removed OAuth client %s', client_id)
|
||||
|
||||
if hasattr(self.oauth, '_clients'):
|
||||
if client_id in self.oauth._clients:
|
||||
@@ -1074,7 +1076,7 @@ class OAuthClientManager:
|
||||
if refreshed_token:
|
||||
# Update the session with new token data
|
||||
session = await OAuthSessions.update_session_by_id(session.id, refreshed_token)
|
||||
log.info(f'Successfully refreshed token for session {session.id}')
|
||||
log.info('Successfully refreshed token for session %s', session.id)
|
||||
return session.token
|
||||
else:
|
||||
log.error(f'Failed to refresh token for session {session.id}')
|
||||
@@ -1242,7 +1244,7 @@ class OAuthClientManager:
|
||||
provider=client_id,
|
||||
token=token,
|
||||
)
|
||||
log.info(f'Stored OAuth session server-side for user {user_id}, client_id {client_id}')
|
||||
log.info('Stored OAuth session server-side for user %s, client_id %s', user_id, client_id)
|
||||
except Exception as e:
|
||||
error_message = 'Failed to store OAuth session server-side'
|
||||
log.error(f'Failed to store OAuth session server-side: {e}')
|
||||
@@ -1369,7 +1371,7 @@ class OAuthManager:
|
||||
if refreshed_token:
|
||||
# Update the session with new token data
|
||||
session = await OAuthSessions.update_session_by_id(session.id, refreshed_token)
|
||||
log.info(f'Successfully refreshed token for session {session.id}')
|
||||
log.info('Successfully refreshed token for session %s', session.id)
|
||||
return session.token
|
||||
else:
|
||||
log.error(f'Failed to refresh token for session {session.id}')
|
||||
@@ -1594,7 +1596,7 @@ class OAuthManager:
|
||||
|
||||
for group_name in user_oauth_groups:
|
||||
if group_name not in all_group_names:
|
||||
log.info(f"Group '{group_name}' not found via OAuth claim. Creating group...")
|
||||
log.info("Group '%s' not found via OAuth claim. Creating group...", group_name)
|
||||
try:
|
||||
new_group_form = GroupForm(
|
||||
name=group_name,
|
||||
@@ -1606,7 +1608,10 @@ class OAuthManager:
|
||||
created_group = await Groups.insert_new_group(creator_id, new_group_form, db=db)
|
||||
if created_group:
|
||||
log.info(
|
||||
f"Successfully created group '{group_name}' with ID {created_group.id} using creator ID {creator_id}"
|
||||
"Successfully created group '%s' with ID %s using creator ID %s",
|
||||
group_name,
|
||||
created_group.id,
|
||||
creator_id,
|
||||
)
|
||||
groups_created = True
|
||||
# Add to local set to prevent duplicate creation attempts in this run
|
||||
@@ -2096,7 +2101,7 @@ class OAuthManager:
|
||||
**({'max_age': cookie_max_age} if cookie_max_age is not None else {}),
|
||||
)
|
||||
|
||||
log.info(f'Stored OAuth session server-side for user {user.id}, provider {provider}')
|
||||
log.info('Stored OAuth session server-side for user %s, provider %s', user.id, provider)
|
||||
else:
|
||||
log.warning(f'Failed to create OAuth session for user {user.id}, provider {provider}')
|
||||
except Exception as e:
|
||||
@@ -2280,11 +2285,14 @@ class OAuthManager:
|
||||
revoked_count += 1
|
||||
|
||||
log.info(
|
||||
f'Back-channel logout: revoked sessions for user {user.id} '
|
||||
f'(email={user.email}, provider={matched_provider}, sessions_deleted={len(sessions)})'
|
||||
'Back-channel logout: revoked sessions for user %s (email=%s, provider=%s, sessions_deleted=%s)',
|
||||
user.id,
|
||||
user.email,
|
||||
matched_provider,
|
||||
len(sessions),
|
||||
)
|
||||
|
||||
log.info(
|
||||
f'Back-channel logout: completed for {len(users_to_logout)} user(s), {revoked_count} revocation(s) set'
|
||||
'Back-channel logout: completed for %s user(s), %s revocation(s) set', len(users_to_logout), revoked_count
|
||||
)
|
||||
return JSONResponse(status_code=200, content={})
|
||||
|
||||
@@ -241,7 +241,7 @@ async def load_tool_module_by_id(tool_id, content=None):
|
||||
exec(content, module.__dict__)
|
||||
if frontmatter is None:
|
||||
frontmatter = extract_frontmatter(content)
|
||||
log.info(f'Loaded module: {module.__name__}')
|
||||
log.info('Loaded module: %s', module.__name__)
|
||||
|
||||
# Create and return the object if the class 'Tools' is found in the module
|
||||
if hasattr(module, 'Tools'):
|
||||
@@ -291,7 +291,7 @@ async def load_function_module_by_id(function_id: str, content: str | None = Non
|
||||
exec(content, module.__dict__)
|
||||
if frontmatter is None:
|
||||
frontmatter = extract_frontmatter(content)
|
||||
log.info(f'Loaded module: {module.__name__}')
|
||||
log.info('Loaded module: %s', module.__name__)
|
||||
|
||||
# Create appropriate object based on available class type in the module
|
||||
if hasattr(module, 'Pipe'):
|
||||
@@ -437,7 +437,7 @@ def install_frontmatter_requirements(requirements: str):
|
||||
if not new_reqs:
|
||||
return
|
||||
|
||||
log.info(f'Installing requirements: {" ".join(new_reqs)}')
|
||||
log.info('Installing requirements: %s', ' '.join(new_reqs))
|
||||
subprocess.check_call(
|
||||
[sys.executable, '-m', 'pip', 'install'] + PIP_OPTIONS + new_reqs + PIP_PACKAGE_INDEX_OPTIONS
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user