How can I squish SQL for log tidiness (dynamically)?

Hello everyone,

I’m dealing with a challenge in my codebase where there are numerous multiline SQL heredocs being logged. While these may be helpful for local debugging, they become a hassle when grepping through logs in production. Searching for specific queries often requires running grep with the -C flag (or restarting grep multiple times with adjusted context), which is inefficient and frustrating for an everyday task like log analysis. For this reason, I want to prioritize tidy, single-line SQL logs that are easier to grep.

While I know that applying .squish to individual queries can help, it’s not developer-friendly and requires everyone to remember to do this consistently. Additionally, it doesn’t always work—for example, when the query begins with a single-line comment.

To address this, I’ve tried dynamically squishing the SQL using a custom LogSubscriber, as shown below. However, I’m concerned that modifying event.payload[:sql] could have unintended side effects when the event is processed elsewhere:

class SQLSquishLogSubscriber < ActiveRecord::LogSubscriber
  def sql(event)
    return super if Rails.env.local?

    sql = event.payload[:sql]
    return super if sql.blank?

    event.payload[:sql] = sql.squish
    super
  end
end

Does anyone have a better solution for ensuring clean, single-line SQL logs without risking side effects or placing extra burden on developers?

Thanks in advance!