{"id":13747333,"url":"https://github.com/brunofacca/active-record-query-trace","last_synced_at":"2025-04-11T04:21:17.630Z","repository":{"id":54795979,"uuid":"1901866","full_name":"brunofacca/active-record-query-trace","owner":"brunofacca","description":"Rails plugin that logs/displays a backtrace of all SQL queries executed by Active Record","archived":false,"fork":false,"pushed_at":"2023-07-27T20:30:59.000Z","size":125,"stargazers_count":965,"open_issues_count":0,"forks_count":70,"subscribers_count":11,"default_branch":"master","last_synced_at":"2024-10-29T14:15:44.366Z","etag":null,"topics":["activerecord","backtrace","ruby","ruby-on-rails"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/brunofacca.png","metadata":{"files":{"readme":"README.md","changelog":"HISTORY.md","contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2011-06-15T18:36:10.000Z","updated_at":"2024-10-24T19:33:09.000Z","dependencies_parsed_at":"2024-06-18T14:02:33.714Z","dependency_job_id":"995a38e0-1803-4220-81ce-1ff9b66c9294","html_url":"https://github.com/brunofacca/active-record-query-trace","commit_stats":{"total_commits":92,"total_committers":28,"mean_commits":"3.2857142857142856","dds":0.7934782608695652,"last_synced_commit":"8f0f586cf22ac202b352459f94bce9c08e31bff8"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brunofacca%2Factive-record-query-trace","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brunofacca%2Factive-record-query-trace/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brunofacca%2Factive-record-query-trace/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brunofacca%2Factive-record-query-trace/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brunofacca","download_url":"https://codeload.github.com/brunofacca/active-record-query-trace/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247078809,"owners_count":20879951,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","backtrace","ruby","ruby-on-rails"],"created_at":"2024-08-03T06:01:25.353Z","updated_at":"2025-04-03T21:08:18.153Z","avatar_url":"https://github.com/brunofacca.png","language":"Ruby","readme":"Displays a backtrace for each query in Rails' development console and log.\nAllows you to track down where queries are executed in your application.\nUseful for performance optimizations and for finding where to start when making\nchanges to a large application.\n\nWhen enabled, every query will be logged like:\n\n```\nD, [2019-03-03T19:50:41.061115 #25560] DEBUG -- : User Load (0.1ms)  SELECT \"users\".* FROM \"users\"\nD, [2019-03-03T19:50:41.062492 #25560] DEBUG -- : Query Trace:\n      app/models/concerns/is_active.rb:11:in `active?'\n      app/models/user.rb:67:in `active?'\n      app/decorators/concerns/status_methods.rb:42:in `colored_status'\n      app/views/shared/companies/_user.html.slim:28:in `block in _app_views_users_html_slim___2427456029761612502_70304705622200'\n      app/views/shared/companies/_user.html.slim:27:in `_app_views_users_html_slim___2427456029761612502_70304705622200'\n```\n\n## Requirements\n- Ruby \u003e= 2.7;\n- Rails 6.0, 6.1, or 7.\n\n## Usage\n\n1. Add the following to your Gemfile:\n   ```ruby\n   group :development do\n     gem 'active_record_query_trace'\n   end\n   ```\n\n2. Create an initializer such as `config/initializers/active_record_query_trace.rb`\nto enable the gem. If you want to customize how the gem behaves, you can add any\ncombination of the following [options](#options) to the initializer as well.\n\n    ```ruby\n    if Rails.env.development?\n      ActiveRecordQueryTrace.enabled = true\n      # Optional: other gem config options go here\n    end\n    ```\n\n3. Restart the Rails development server.\n\n## Options\n\n#### Backtrace level\nThere are three levels of debug.\n\n- `:app` - includes only application trace lines (files in the `Rails.root` directory);\n- `:rails` - includes all trace lines except the ones from the application (all files except those in `Rails.root`).\n- `:full` - full backtrace (includes all files), useful for debugging gems.\n\n```ruby\nActiveRecordQueryTrace.level = :app # default\n```\n\nIf you need more control you can provide a custom bactrace cleaner using the `:custom` level. For example:\n\n```ruby\nActiveRecordQueryTrace.level = :custom\nrequire \"rails/backtrace_cleaner\"\nActiveRecordQueryTrace.backtrace_cleaner = Rails::BacktraceCleaner.new.tap do |bc|\n  bc.remove_filters!\n  bc.remove_silencers!\n  bc.add_silencer { |line| line =~ /\\b(active_record_query_trace|active_support|active_record|another_gem)\\b/ }\nend\n```\n\nIt's not necessary to create an instance of `Rails::BacktraceCleaner`, you can use any object responding to `#clean` or even\na lambda/proc:\n\n```ruby\nActiveRecordQueryTrace.backtrace_cleaner = -\u003e(trace) {\n  trace.reject { |line| line =~ /\\b(active_record_query_trace|active_support|active_record|another_gem)\\b/ }\n}\n```\n\n#### Display the trace only for read or write queries\nYou can choose to display the backtrace only for DB reads, writes or both.\n\n- `:all` - display backtrace for all queries;\n- `:read` - display backtrace only for DB read operations (SELECT);\n- `:write` - display the backtrace only for DB write operations (INSERT, UPDATE, DELETE).\n\n```ruby\nActiveRecordQueryTrace.query_type = :all # default\n```\n\n#### Suppress DB read queries\nIf set to `true`, this option will suppress all log lines generated by DB\nread (SELECT) operations, leaving only the lines generated by DB write queries\n(INSERT, UPDATE, DELETE). **Beware, the entire log line is suppressed, not only\nthe backtrace.** Useful to reduce noise in the logs (e.g., N+1 queries) when you\nonly care about queries that write to the DB.\n\n```ruby\nActiveRecordQueryTrace.suppress_logging_of_db_reads = false # default\n```\n\n#### Ignore cached queries\nBy default, a backtrace will be logged for every query, even cached queries that\ndo not actually hit the database. You might find it useful not to print the backtrace\nfor cached queries:\n\n```ruby\nActiveRecordQueryTrace.ignore_cached_queries = true # Default is false.\n```\n\n#### Limit the number of lines in the backtrace\nIf you are working with a large app, you may wish to limit the number of lines\ndisplayed for each query.  If you set `level` to `:full`, you might want to set\n`lines` to `0` so you can see the entire trace.\n\n```ruby\nActiveRecordQueryTrace.lines = 10 # Default is 5. Setting to 0 includes entire trace.\n```\n\n#### Colorize the backtrace\nTo colorize the output:\n\n```ruby\nActiveRecordQueryTrace.colorize = false           # No colorization (default)\nActiveRecordQueryTrace.colorize = :light_purple   # Colorize in specific color\n```\n\nValid colors are: `:black`, `:red`, `:green`, `:brown`, `:blue`, `:purple`, `:cyan`,\n`:gray`, `:dark_gray`, `:light_red`, `:light_green`, `:yellow`, `:light_blue`,\n`:light_purple`, `:light_cyan`, `:white`.\n\n## Authors\n\n- **Cody Caughlan** - Original author.\n- **Bruno Facca** - Current maintainer. [LinkedIn](https://www.linkedin.com/in/brunofacca/)\n\n## Contributing\n\n#### Bug reports\n\nPlease use the issue tracker to report any bugs.\n\n#### Test environment\n\nThis gem uses RSpec for testing. You can run the test suite by executing the\n`rspec` command. It has a decent test coverage and the test suite takes less than\na second to run as it uses an in-memory SQLite DB.\n\n#### Developing\n\n1. Create an issue and describe your idea\n2. Fork it\n3. Create your feature branch (`git checkout -b my-new-feature`)\n4. Implement your changes;\n5. Run the test suite (`rspec`)\n6. Commit your changes (`git commit -m 'Add some feature'`)\n7. Publish the branch (`git push origin my-new-feature`)\n8. Create a Pull Request\n\n## License\n\nReleased under the [MIT License](https://opensource.org/licenses/MIT).\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrunofacca%2Factive-record-query-trace","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrunofacca%2Factive-record-query-trace","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrunofacca%2Factive-record-query-trace/lists"}