# 添加对查询日志注释前置的支持

**URL:** https://meta.discourse.org/t/adding-support-for-prepending-query-log-comments/387826
**Category:** Feature
**Created:** [2025年十一月6日 22:23 UTC](https://meta.discourse.org/t/adding-support-for-prepending-query-log-comments/387826 "2025-11-06T22:23:33Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Mudit\_Maheshwari](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mudit_maheshwari/32/331278_2.png) [@Mudit\_Maheshwari](https://meta.discourse.org/u/Mudit_Maheshwari)
#### Post date: [2025年十一月6日 22:23 UTC](https://meta.discourse.org/t/adding-support-for-prepending-query-log-comments/387826/1 "2025-11-06T22:23:33Z")

</div>

### 为什么？

查询日志标签对于调试非常有用，但有一个问题：在处理冗长复杂的查询时，PostgreSQL 和其他数据库经常会截断日志。发生这种情况时，来自查询标签（默认情况下附加在末尾）的有价值的上下文信息会被切断，使得难以追踪应用程序的哪个部分生成了查询。

通过启用 `prepend_comment`，查询标签会被移到 SQL 查询的开头而不是末尾。这样可以确保即使查询在日志中被截断，您仍然可以看到关键的调试信息，例如控制器名称、操作名称和作业信息。

### 如何？

该功能需要选择加入，并遵循 Discourse 现有的配置模式：

1. 启用查询日志标签（如果尚未启用）：`RAILS_QUERY_LOG_TAGS=1`
2. 启用前置注释：`RAILS_QUERY_LOG_PREPEND_COMMENT=1`

该功能仅在同时满足这两个条件时激活：必须启用查询日志标签，并且必须设置前置注释标志。这可以保持向后兼容性，并为操作员提供对其日志配置的精细控制。

实现 PR：[Add support for ActiveRecord::QueryLogs.prepend\_comment by mudit-1996 · Pull Request #35888 · discourse/discourse · GitHub](https://github.com/discourse/discourse/pull/35888)
