Why do ActiveRecord queries from console print sql statements? - ruby-on-rails

I'm using pry, and ActiveRecord queries from the console always print out their corresponding SQL statements. I don't want this behavior. Googling around I only see that this must be explicitly achieved by setting the ActiveRecord logger to standard out.
Is this the default behavior of pry or is it the result of something I set that I forgot about? And how can I stop it?

Try looking for a .irbrc file in the project root or your home directory. You may see this or similar:
ActiveRecord::Base.logger = Logger.new(STDOUT)
Sometimes this ends up in another script that gets included in .irbrc.
YMMV, but I really like having SQL logged to the console. To each his own...

If you are using Rails 3.1+ this is now the default behaviour. Check here Disable Rails SQL logging in console

Related

Starting Byebug with a block

I'm wondering if it's possible to start a Byebug session giving a starting point from a Rails console. I know I can insert a byebug statement wherever I want and start debugging, but I'd like to do something like this:
Byebug.start do
# entry point
User.find(12).problematic_method
end
Thanks.
I opened the class and override the problematic_method inside the Rails console and added the byebug statement where I wanted it. This way I don't have to change the running production code (I forgot to mention above I want to debug in production).
This workaround will be enough for my purposes. The only problem is that you don't have the debug code listing available for that method, but its fine.
That is not possible. What you can do, is write your code inside a .rb file and debug that file/script using byebug.

How do I turn off Rails SQL logging in test?

For some reason (probably an updated gem) Rails is logging all my SQL commands now. I run autotest and they are being spammed during tests also. How do I turn it off?
I tried add this to config/environments/test.rb but it didn't work. logger was already nil.
# ActiveRecord::Base.logger = nil
# ActiveRecord::Base.logger.level = 1
Rails 4.0.0
Ok I found it. This worked:
config.after_initialize do
ActiveRecord::Base.logger = nil
end
Another thing you can do is call this code at runtime, it doesn't need to be in a config file.
For example, if you put in your specific test case
# test/functionals/application_controller_test.rb for example
ActiveRecord::Base.logger = nil
It would work just as well, and this way you can toggle it at runtime. Useful if you only want to stifle a few lines of code or a block.
In case someone wants to actually knock out SQL statement logging (without changing logging level, and while keeping the logging from their AR models):
The line that writes to the log (in Rails 3.2.16, anyway) is the call to 'debug' in lib/active_record/log_subscriber.rb:50.
That debug method is defined by ActiveSupport::LogSubscriber.
So we can knock out the logging by overwriting it like so:
module ActiveSupport
class LogSubscriber
def debug(*args, &block)
end
end
end

How do I dump Pry output to a file or Vim?

I have a Rails application, and I'm trying to export data, but directly through Pry because I only need to do it once.
Is this possible with Pry? I looked at the documentation but doesn't seem like there's an easy way to dump console data anywhere.
I have a hash, with nested hashes/objects, which I need to send over to a 3rd party for work with an API. They need a dump of the data so they can set up the receiving end of my call. I'm just going to do this in Ruby now, but it would have made more sense to dump the data through PRY, rather than edit my ruby object to dump the data, which I only need once.
If you can start the server from a local command-line, or SSH to the host and run an instance there, you can use Pry for this. Basically you need to add these lines to your code at the appropriate place:
require 'pry-debugger'; binding.pry
which will stop your code and put you at the Pry prompt. At that point you can enter something like:
require 'json'
File.write('test.data', hash.to_json)
Read the Pry.debugger documentation for information about using Pry with remote Rails sessions, which might work better for you.
You can also export any string into a file (here output.txt):
x = 'something funky'
.echo '#{x}' > output.txt
Just be careful with quotes in the string. These may lead to problems in the shell.

Logging all method calls in a Rails app

Is there an easy way to log all method calls in a Rails app?
My main use for this would be in testing (and in debugging tests). I want to have more of a history than a stacktrace provides (for instance, when running rspec with the '-b' option).
It's easy to do. Just add 5 lines of code into your script/server:
#!/usr/bin/env ruby
set_trace_func proc {
|event, file, line, id, binding, classname|
if event == "call" or event == "return"
printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
end
}
require File.expand_path('../../config/boot', __FILE__)
require 'commands/server'
It's described at http://phrogz.net/ProgrammingRuby/ospace.html#tracingyourprogramsexecution
Your application will become quite slow and you might get more output than you want. You can easily add more conditions on file/class/function names to avoid printing unwanted stuff.
Perftools might give you what you're looking for. It analyzes the entire process and can give you a graphical view that looks something like this. Rack perftools profiler is a rubygem that uses perftools and makes it easy to integrate with a Rails application, so I would recommend going with that if you want to try it.
Firstly stacktrace IS every method call that was on the stack at the time an error occurred, what other history could you want besides this?
Secondly, to answer your question, no there is no easy way to log all method calls. You could up your log level all the way to debug which should give you more stuff in the logs, but this will only be things that someone has actually chosen to log, unrelated to method calls.
It probably wouldn't be that difficult to patch ruby in such a way that every method call will print some log statements before and after the method execution, but this will once again be similar to what a stack trace would give you anyway and potentially less since you won't get line numbers etc.
If you want more info than the stack trace, logging is the way most people would do it.

"Correct" way to write log output from a background (Resque) job?

Resque jobs are just plain old ruby objects. I can use puts calls inside them to produce output into the console, or I can instantiate a standard Ruby Logger class with STDOUT and use that.
But is there a correct approach to logging in Rails, from places that aren't controllers or models? I see Rails.logger returns a BufferedLogger, but when I call info or warn etc on it, nothing happens. If I call flush on it, it just returns an empty array and nothing is output.
What's the convention here?
I'm not really sure that there is a convention. I had a pretty ugly logging system up until just recently. Now I use lumber to integrate log4r with Rails. That really made logging much nicer because I now have named loggers (e.g., logger matches the class name -- great for filtering output) and I can control log levels on a per-logger (i.e., per-class) basis.
There's also a GELF adapter for log4r if you want to use graylog2 to aggregate your logs.

Resources