aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/log_subscriber.rb
Commit message (Collapse)AuthorAgeFilesLines
* Find query_source_location using lazy EnumeratorJohn Hawthorn2019-04-151-1/+1
| | | | | | | | | This way, we only need to filter the backtrace up to the first non-noise stack frame. This also updates noise to be able to deal with being passed a lazy enum. We don't need this anywhere, but it seemed better for this to be consistent.
* Use backtrace cleaner to clean up backtrace for verbose query logsLachlan Sylvester2018-08-141-9/+5
|
* Don't expose configuration for a test.Kasper Timm Hansen2018-07-281-21/+9
| | | | Clean up some concepts in the code while we're here.
* Avoid logging ActiveRecord::LogSubscriber as the query source when the ↵lsylvester2018-07-281-12/+9
| | | | source is ignored (#33455)
* Enable `Lint/StringConversionInInterpolation` rubocop ruleRyuta Kamizono2018-05-211-1/+1
| | | | | To prevent redundant `to_s` like https://github.com/rails/rails/pull/32923#discussion_r189460008 automatically in the future.
* Return back "/" to the end of RAILS_GEM_ROOTbogdanvlviv2018-04-201-1/+1
| | | | | | | | | | | - The "/" was removed in 40bdbce191ad90dfea43dad51fac5c4726b89392 during refactoring. It may cause regression since looks like was added intentionaly because it is possible that a name of any another gem can start with /rails/, so slash was added to ensure that it is "rails" gem. I would like to backport this to `5-2-stable` too. - Use `__dir__` instead of `__FILE__`. Follow up #29176.
* Remove verbose_query_logs from new_framework_defaults_5_2.rbEugene Kenny2017-12-201-2/+1
| | | | | | | | | | | The `app:update` rake task will regenerate `development.rb` so that it contains this option; that means we're currently adding it to existing apps in two places, which is unnecessary and confusing. Also: - Remove inaccurate comment about which stack frames are ignored - Clarify that the feature uses `caller_locations`, not `caller` - Remove unused return value in `extract_callstack`
* Log call site for all queriesOlivier Lacan2017-12-131-0/+42
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This new ActiveRecord configuration option allows you to easily pinpoint what line of application code is triggering SQL queries in the development log by appending below each SQL statement log the line of Ruby code that triggered it. It’s useful with N+1 issues, and to locate stray queries. By default this new option ignores Rails and Ruby code in order to surface only callers from your application Ruby code or your gems. It is enabled on newly generated Rails 5.2 applications and can be enabled on existing Rails applications: ```ruby Rails.application.configure do # ... config.active_record.verbose_query_logs = true end ``` The `rails app:upgrade` task will also add it to `config/development.rb`. This feature purposely avoids coupling with ActiveSupport::BacktraceCleaner since ActiveRecord can be used without ActiveRecord. This decision can be reverted in the future to allow more configurable backtraces (the exclusion of gem callers for example).
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
|
* Should use the same connection in using query cacheRyuta Kamizono2017-06-291-4/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `test_cache_is_available_when_using_a_not_connected_connection` is always failed if running only the test since #29609. ``` % ARCONN=mysql2 be ruby -w -Itest test/cases/query_cache_test.rb -n test_cache_is_available_when_using_a_not_connected_connection Using mysql2 Run options: -n test_cache_is_available_when_using_a_not_connected_connection --seed 15043 F Finished in 0.070519s, 14.1806 runs/s, 28.3612 assertions/s. 1) Failure: QueryCacheTest#test_cache_is_available_when_using_a_not_connected_connection [test/cases/query_cache_test.rb:336]: 2 instead of 1 queries were executed. Queries: SELECT `tasks`.* FROM `tasks` WHERE `tasks`.`id` = ? LIMIT ? SET NAMES utf8 COLLATE utf8_unicode_ci, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483. Expected: 1 Actual: 2 1 runs, 2 assertions, 1 failures, 0 errors, 0 skips ``` This failure is due to `LogSubscriber` will use not connected `ActiveRecord::Base.connection` even if `Task.connection` is connected. I fixed to always pass `type_casted_binds` to log subscriber to avoid the issue.
* Fix `LogSubscriber` to allow legacy `binds`Ryuta Kamizono2017-03-231-11/+7
| | | | Follow up of #27939.
* Fix indentKouhei Sutou2017-01-111-1/+1
|
* Use old typecasting method if no type casted binds are passed inAaron Patterson2016-10-201-1/+10
| | | | | | | | | | | | | Query cache doesn't type cast bind parameters since it isn't actually querying the database, so it can't pass those values in. Type casting in the query cache method would cause the values to be type cast twice in the case that there is a cache miss (since the methods it calls will type cast *again*). If logging is disabled, then adding the type cast code to the query cache method will needlessly typecast the values (since the only reason those values are type cast is for display in the logs). Fixes #26828.
* render_bind should be privateAaron Patterson2016-10-191-10/+10
|
* Merge pull request #24963 from fertapric/recover-db-runtime-on-production-logsEileen M. Uchitelle2016-10-061-2/+1
|\ | | | | Fix database runtimes on production log
| * Fix DB runtimes on production logFernando Tapia Rico2016-05-111-2/+1
| | | | | | | | | | | | | | | | Rails default production configuration establishes "info" as log level. Due to the changes included on commit 191facc857bb4fb52078fb544c6bc1613a81cc80, db runtimes were not being collected if the log level was different than "debug", and 0.0 ms was the runtime reported on production logs.
* | Preserve cached queries name in AS notificationsJean Boussier2016-09-221-0/+1
| |
* | Remove odd ivar from ActiveRecord::LogSubscriberEugene Kenny2016-08-151-5/+0
| | | | | | | | | | This was used to switch the output colour between log lines, but now the output colour is based on the type of statement being logged instead.
* | applies remaining conventions across the projectXavier Noria2016-08-061-15/+15
| |
* | normalizes indentation and whitespace across the projectXavier Noria2016-08-061-28/+28
| |
* | Pass `type_casted_binds` to log subscriber for logging bind values properlyRyuta Kamizono2016-07-191-14/+8
| | | | | | | | Address to https://github.com/rails/rails/commit/5a302bf553af0e6fedfc63299fc5cd6e79599ef3#commitcomment-18288388.
* | Logging type casted bindsRyuta Kamizono2016-07-191-1/+5
| | | | | | | | Fixes #22398.
* | Fix ActiveRecord::LogSubscriber edge caseJon Moss2016-05-101-1/+5
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If an attribute was of the binary type, and also was a Hash, it would previously not be logged, and instead raise an error saying that `bytesize` was not defined for the `attribute.value` (a `Hash`). Now, as is done on 4-2-stable, the attribute's database value is `bytesize`d, and then logged out to the terminal. Reproduction script: ```ruby require 'active_record' require 'minitest/autorun' require 'logger' ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') ActiveRecord::Base.logger = Logger.new(STDOUT) ActiveRecord::Schema.define do create_table :posts, force: true do |t| t.binary :preferences end end class Post < ActiveRecord::Base serialize :preferences end class BugTest < Minitest::Test def test_24955 Post.create!(preferences: {a: 1}) assert_equal 1, Post.count end end ```
* Removes potentially quadratic Regexp from ActiveRecord::LogSubscriber#sql_colorClifford T. Matthews2016-02-231-1/+1
|
* Merge pull request #20921 from pboling/fix-sql-colors-in-log-subscriberRafael Mendonça França2015-09-091-7/+27
|\ | | | | | | Fix and Improve sql logging coloration in `ActiveRecord::LogSubscriber`.
| * Remove extraneous `then` from case statementPeter Boling2015-07-171-12/+19
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | - CR feedback from @egilburg Additionally - Move logic for colorizing the payload name into a separate method - Make some `ActiveRecord::LogSubscriber` instance methods private for clarity: - `colorize_payload_name` - `sql_color` - `logger` - Improve Changelog Documentation GH #20885
| * Improve sql logging coloration in `ActiveRecord::LogSubscriber`.Peter Boling2015-07-171-7/+21
|/ | | | | | | | | | | | | | | | | | | | | | | - Improves coloring for statements like: # Become WHITE SELECT * FROM ( SELECT * FROM mytable FOR UPDATE ) ss WHERE col1 = 5; LOCK TABLE table_name IN ACCESS EXCLUSIVE MODE; # Becomes RED ROLLBACK - Reinstates the coloration of the `payload[:name]`. Instead of simple alternating colors, adds meaning: - `MAGENTA` for `"SQL"` or `blank?` payload names - `CYAN` for Model Load/Exists - Introduces specs for sql coloration. - Introduces specs for payload name coloration. GH#20885
* More granular console SQL colorationChris Tonkinson2015-06-231-8/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This new coloration approach makes it easier to scan the rails console for specific types of activity with more fine-grained visual cues. Virtual terminal ANSI color escape codes are used when displaying SQL statements in the rails console. The former implementation alternates line prefix information (including the statement name and execution latency) between CYAN and MAGENTA. This visually differentiates any SQL statements in the log and is useful for quickly scanning for database activity. While a great idea and a solid foundation, alternating between just two colors on an even/odd basis (much like striping an HTML table) can be improved upon. This patch replaces the even/odd striping with a more comprehensive scheme that applies coloration based on the type of statement being run. Every statement logged has its prefix (name and latency) colored white (as the statement body was previously). The statement body is now colored according to the nature of the statement: - INSERT statements are GREEN (symbolic of creation or genesis) - SELECT statements are BLUE (typically used for informational displays, as SELECT statements do not normally have side-effects) - DELETE statements are RED (commonly used to indicate the danger of a destructive action) - UPDATE statements are YELLOW (it's like a less extreme RED :P) - TRANSACTION statements are CYAN (arbitrary) - and any other statements are MAGENTA (again, arbitrary)
* Don't invoke sql_runtime if logger is not set to infoeileencodes2015-04-041-1/+2
| | | | | | | | | `sql_runtime` was getting invoked even when the logger was set to fatal. This ensures that does not happen by checking that the logger is set to info level before logging the view runtime. This reduces the number of times `sql_runtime` is called for integration tests with a fatal logger from 6 to 2.
* Remove Relation#bind_paramsSean Griffin2015-01-271-13/+7
| | | | | | | | `bound_attributes` is now used universally across the board, removing the need for the conversion layer. These changes are mostly mechanical, with the exception of the log subscriber. Additional, we had to implement `hash` on the attribute objects, so they could be used as a key for query caching.
* Stop special casing null binary data in loggingSean Griffin2015-01-101-2/+2
| | | | | | There's very little value in logging "<NULL binary data>" instead of just "nil". I'd like to remove the column from the equation entirely, and this case is preventing us from doing so.
* Invert the if/else clauseRafael Mendonça França2014-05-041-1/+1
| | | | The case where the value is present is more common
* Fix exception when logging SQL w/ nil binary value.James Coleman2014-05-021-1/+1
|
* type casted PG bytea values are represented as a Hash.Yves Senn2013-11-091-0/+2
| | | | | This is a follow up to: 97f0d9a0dd12e7ad634815eecfeff866f64aad92 This change is also related to: df0edafac9eb47cd971970a9e7b13a3eaddf214e
* Cleanup identitymap logger from LogSubscriberArthur Neves2013-09-281-11/+2
| | | | Also renaming ivar @odd_or_even to @odd
* log the sql that is actually sent to the databaseNeeraj Singh2013-06-191-1/+1
| | | | | | | | | | | | If I have a query that produces sql `WHERE "users"."name" = 'a b'` then in the log all the whitespace is being squeezed. So the sql that is printed in the log is `WHERE "users"."name" = 'a b'`. This can be confusing. This commit fixes it by ensuring that whitespace is not squeezed. fixes #10982
* removes calls to AR::Runtime.instanceXavier Noria2013-04-131-2/+2
| | | | | | | | | | Registries have class-level accessors to write clean code, let's use them. This makes style uniform also with existing usage in ScopeRegistry and InstrumentationRegistry. If performance of the method_missing callback was ever considered to be a concern, then we should stop using it altogether and probably remove the callback. But while we have the feature we should use it.
* Created a runtime registry for thread local variables in active record.wangjohn2013-04-091-2/+2
|
* Ignore binds payload with nil column in AR log subscriberCarlos Antonio da Silva2013-01-081-5/+7
| | | | | | | | | | | | | Some tests were raising the following error: Could not log "sql.active_record" event. NoMethodError: undefined method `type' for nil:NilClass` Due to the way binds were being logged, the column info was considered always present, but that is not true for some of the tests listed in the issue. Closes #8806.
* Skip binary data with binds test for mysql2, fix buildCarlos Antonio da Silva2012-12-141-1/+1
| | | | | | | Mysql2 doesn't support binds, which means no binds payload is set when logging, so the logic to render binary data differently here doesn't work. Introduced in 99d142a9375f9ba1960863b3cc745265aa9a14df.
* Do not log the binding values for binary columns.Matthew M. Boedicker2012-12-141-1/+11
| | | | They tend to be large and not very useful in the log.
* Optimize log subscribers to check if the log level is sufficient before ↵Brian Durand2012-09-301-4/+6
| | | | performing an operations.
* implements a much faster auto EXPLAIN, closes #3843 [José Valim & Xavier Noria]Xavier Noria2011-12-041-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | This commit vastly reduces the impact of auto explain logging when enabled, while keeping a negligible cost when disabled. The first implementation was based on the idea of subscribing to "sql.active_record" when needed, and unsubscribing once done. This is the idea behind AR::Relation#explain. Subscribe, collect, unsubscribe. But with the current implementation of notifications unsubscribing is costly, because it wipes an internal cache and that puts a penalty on the next event. So we are switching to an approach where a long-running subscriber is listening. Instead of collecting the queries with a closure in a dedicated subscriber, now we setup a thread local. If the feature is disabled by setting the threshold to nil, the subscriber will call a method that does nothing. That's totally cheap.
* Make the identity map use the instrumentation infrastructure so we can style ↵David Heinemeier Hansson2011-05-011-0/+9
| | | | the messages nicely with colors (FIXME: Can someone look into why the test is not working?)
* sql logger ignores schema statementsAaron Patterson2011-03-281-0/+3
|
* fixing tests on 1.8, using a list of lists because order is importantAaron Patterson2011-02-101-1/+3
|
* simplify bind parameter loggingAaron Patterson2011-02-101-3/+1
|
* bind parameters are logged to debug logAaron Patterson2011-02-101-4/+12
|