Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only enable verbose_query_logs in Rails server #31690

Merged

Conversation

olivierlacan
Copy link
Contributor

@olivierlacan olivierlacan commented Jan 12, 2018

Should fix #31688 unless someone can point me to a better way to achieve
this goal. Essentially David's point was that verbose query logging when
enabled in Rails console tends to make things very noisy.

That's especially true if we display absolute paths to callsites which
sadly is still the case when we detect a caller that isn't part of the
Rails application — think gems.

Related to #26815.

I haven't tested this locally yet so don't merge until the build has run and we can test it out a bit more.

@palkan
Copy link
Contributor

palkan commented Jan 12, 2018

@olivierlacan

a better way to achieve
this goal

What about setting verbose_query_logs to false in ActiveRecord::Railtie#console?

@@ -94,7 +94,7 @@ def logger
def debug(progname = nil, &block)
return unless super

if ActiveRecord::Base.verbose_query_logs
if defined?(Rails::Server) && ActiveRecord::Base.verbose_query_logs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thought: sometimes it's really useful to conditionally enable verbose logging in tests (and, maybe, console and other tasks too), i.e. manually turn it on and off; this hack make it impossible.

Moreover, ActiveRecord could be used without Rails at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I don't love this first approach either. I'd rather it be configurable and off by default.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Active Record should not know anything about railties. Maybe we should move this code to the railtie?

initializer 'set_verbose_logs' do
  ActiveRecord::Base.verbose_query_logs = defined?(Rails::Server) && config.active_record.verbose_query_logs
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think defined?(Rails::Server) is still the wrong approach, even if we move it to the Railtie. It will disable the feature in too many places: standalone web servers (puma), background job servers (sidekiq), even other built-in Rails contexts (bin/rails runner)...

We should disable it when the console is used, not enable it when the server is used. I like @palkan's suggestion of using the console do block to do that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we accomplish this by checking whether the FD that the logger is outputting to is interactive? (basically out.repond_to?(:tty?) && out.tty?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, because both the server and console commands multiplex their log output to an interactive stream (STDOUT for the server and STERR for the console), but we only want to disable verbose query logs on the console.

Ideally we'd always write the verbose query logs to the log file and exclude them from STDOUT/STDERR, but I don't think that's possible with how the multiplexing is implemented.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the console block for now in the railtie. If we have another case where we need it disabled too we can revisit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rafaelfranca Updating the PR with your suggestion now.

@dhh dhh added this to the 5.2.0 milestone Jan 18, 2018
@olivierlacan olivierlacan force-pushed the no-verbose-query-logs-in-console branch from 45ce745 to 4e0cc7a Compare January 18, 2018 20:10
@@ -222,5 +222,11 @@ class Railtie < Rails::Railtie # :nodoc:
end
end
end

initializer "active_record.set_verbose_logs" do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rafaelfranca Let me know if this is what you had in mind?

Should fix rails#31688 unless someone can point me to a better way to achieve
this goal. Essentially David's point was that verbose query logging when
enabled in Rails console tends to make things very noisy.

That's especially true if we display absolute paths to callsites which
sadly is still the case when we detect a caller that isn't part of the
Rails application — think gems.

Discussed this with both @matthewd and @rafaelfranca and went back and
forth between enabling if defined?(Rails::Server) or this implementation
and this one makes more sense for now.

Long term I think it'll make sense to let people override this default
disabling in Rails Console because they might want to use the feature
but for now it feels like the correct default behavior.
@olivierlacan olivierlacan force-pushed the no-verbose-query-logs-in-console branch from 4e0cc7a to 1044e8b Compare January 23, 2018 15:23
@olivierlacan
Copy link
Contributor Author

@rafaelfranca Updated to use the console block in AR's railtie. Let me know if that works.

Tests look like they're good aside from build errors.

@rafaelfranca rafaelfranca merged commit 554dcc4 into rails:master Jan 24, 2018
@rafaelfranca
Copy link
Member

Awesome! Thank you for working on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Turn off Active Record's verbose_query_logs when using the console
6 participants