feat(logging): add LOG_ADMIN_HIDE setting and update logging functionality
Introduce a new configuration option `LOG_ADMIN_HIDE` to control the visibility of admin-generated events in the logs. Update the logging retrieval functions to respect this setting, ensuring that admin actions can be hidden from the "All message logs" UI and CSV exports. Additionally, enhance the README with detailed logging configuration options for better clarity.
This commit is contained in:
@@ -171,9 +171,17 @@ async def view_all_logs_handler(callback: types.CallbackQuery,
|
||||
await callback.answer("Error processing request.", show_alert=True)
|
||||
return
|
||||
|
||||
hide_admin_events = bool(settings.LOG_ADMIN_HIDE)
|
||||
logs_models = await message_log_dal.get_all_message_logs(
|
||||
session, settings.LOGS_PAGE_SIZE, page_idx * settings.LOGS_PAGE_SIZE)
|
||||
total_logs_count = await message_log_dal.count_all_message_logs(session)
|
||||
session,
|
||||
settings.LOGS_PAGE_SIZE,
|
||||
page_idx * settings.LOGS_PAGE_SIZE,
|
||||
hide_admin_events=hide_admin_events,
|
||||
)
|
||||
total_logs_count = await message_log_dal.count_all_message_logs(
|
||||
session,
|
||||
hide_admin_events=hide_admin_events,
|
||||
)
|
||||
|
||||
await _display_formatted_logs(
|
||||
target_message=callback.message,
|
||||
@@ -344,8 +352,12 @@ async def export_logs_csv_handler(callback: types.CallbackQuery,
|
||||
try:
|
||||
# Get all logs (limit to 10000 for performance)
|
||||
logs_models = await message_log_dal.get_all_message_logs(
|
||||
session, limit=10000, offset=0)
|
||||
|
||||
session,
|
||||
limit=10000,
|
||||
offset=0,
|
||||
hide_admin_events=bool(settings.LOG_ADMIN_HIDE),
|
||||
)
|
||||
|
||||
if not logs_models:
|
||||
await callback.message.answer(_(
|
||||
"admin_logs_csv_no_data"
|
||||
@@ -355,7 +367,7 @@ async def export_logs_csv_handler(callback: types.CallbackQuery,
|
||||
# Create CSV content
|
||||
csv_buffer = io.StringIO()
|
||||
csv_writer = csv.writer(csv_buffer, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
|
||||
|
||||
|
||||
# Write header
|
||||
headers = [
|
||||
_("admin_csv_header_log_id"),
|
||||
@@ -374,16 +386,16 @@ async def export_logs_csv_handler(callback: types.CallbackQuery,
|
||||
_("admin_csv_header_raw_update_preview"),
|
||||
])
|
||||
csv_writer.writerow(headers)
|
||||
|
||||
|
||||
# Write data rows
|
||||
for log in logs_models:
|
||||
# Format timestamp
|
||||
timestamp_str = log.timestamp.strftime('%Y-%m-%d %H:%M:%S UTC') if log.timestamp else ''
|
||||
|
||||
|
||||
# Clean content and raw_update_preview (remove newlines and quotes for CSV)
|
||||
content_clean = (log.content or '').replace('\n', ' ').replace('\r', ' ').strip()
|
||||
raw_update_clean = (log.raw_update_preview or '').replace('\n', ' ').replace('\r', ' ').strip()
|
||||
|
||||
|
||||
row = [
|
||||
log.log_id or '',
|
||||
timestamp_str,
|
||||
@@ -400,21 +412,21 @@ async def export_logs_csv_handler(callback: types.CallbackQuery,
|
||||
raw_update_clean,
|
||||
])
|
||||
csv_writer.writerow(row)
|
||||
|
||||
|
||||
# Create file
|
||||
csv_content = csv_buffer.getvalue()
|
||||
csv_buffer.close()
|
||||
|
||||
|
||||
# Generate filename with current timestamp
|
||||
now = datetime.now()
|
||||
filename = f"message_logs_{now.strftime('%Y%m%d_%H%M%S')}.csv"
|
||||
|
||||
|
||||
# Send as document
|
||||
csv_file = types.BufferedInputFile(
|
||||
csv_content.encode('utf-8-sig'), # BOM for Excel compatibility
|
||||
filename=filename
|
||||
)
|
||||
|
||||
|
||||
await callback.message.answer_document(
|
||||
csv_file,
|
||||
caption=_(
|
||||
@@ -423,7 +435,7 @@ async def export_logs_csv_handler(callback: types.CallbackQuery,
|
||||
date=now.strftime('%Y-%m-%d %H:%M:%S')
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error exporting logs to CSV: {e}", exc_info=True)
|
||||
await callback.message.answer(_(
|
||||
|
||||
@@ -645,8 +645,20 @@ async def yookassa_webhook_route(request: web.Request):
|
||||
yookassa_service,
|
||||
lknpd_service)
|
||||
if not processed:
|
||||
await session.rollback()
|
||||
return web.Response(status=503, text="yookassa_processing_failed_retry")
|
||||
# process_successful_payment uses False for permanent business failures
|
||||
# (e.g. user not found / metadata issues) and may have already updated
|
||||
# the payment status. Commit the status and ACK the webhook to stop
|
||||
# indefinite provider retries.
|
||||
try:
|
||||
await session.commit()
|
||||
except Exception:
|
||||
await session.rollback()
|
||||
logging.exception(
|
||||
"Failed to commit failure status for YooKassa payment %s",
|
||||
payment_dict_for_processing.get("id"),
|
||||
)
|
||||
return web.Response(status=503, text="yookassa_processing_failed_retry")
|
||||
return web.Response(status=200, text="ok")
|
||||
await session.commit()
|
||||
else:
|
||||
logging.warning(
|
||||
|
||||
Reference in New Issue
Block a user