[Proposal] Add config option to log the original call site of a DB query in more detail

(Hey there! This is my first time proposing a new Rails feature; feedback very much appreciated!)

I find the verbose_query_logs setting extremely useful for debugging, and I’d like to propose an additional configuration setting to make it more detailed when desired.

Right now, verbose_query_logs logs a single line, describing the specific call site where a given DB query originated. This is great, but there are times when even more detail about the call site would be useful – for example, when the DB query happens in a method that is called from many other places throughout the codebase.

verbose_query_logs_stack_depth would allow a developer to configure how many lines of the call site stack trace should be displayed. For example, a value of 3 would display the 3 immediate ancestors of the DB query. With this setting, a developer could increase the amount of detail that is printed about each DB query, helping them to more quickly locate the source of a problematic query and thus to fix it.

It looks like a fairly simple code update, so I already have a branch ready (GitHub - rofreg/rails at add-verbose_query_logs_stack_depth), but the Rails contribution guide suggested that I share my proposed change before I open a PR, so here I am :slight_smile: I realize this is a fairly small and specific suggestion, but I think it might be an especially useful one. Looking forward to any feedback!

1 Like

Looks like a good idea. Feel free to open a PR, especially since you have already done all the work.

Another existing option. You can just use logster (both in production or development) and get the backtraces

Hm, I can’t tell if logster actually does what I’m looking for – I don’t see any mentions in the docs/code about logging traces for DB queries specifically. verbose_query_logs is really useful for that particular use case, which is why I wanted to explore extending it with the ability to show more detail.

Someone else (I think they deleted their post from the thread?) recommended active-record-query-trace, which does seem like a more powerful version of what I’m suggesting, and looks very cool. Maybe that’s a sign that at least for now, this shouldn’t be a part of Rails itself, and is more appropriate as an optional additional gem.

You get traces on every log line.

To get traces on SQL though I would recommend just using rack-mini-profiler.

1 Like