Possible to mute irrelevant error messages? - ruby-on-rails

If I erroneously would call a private method in the console, I'm getting 25 lines of error messages like the following:
from /Users/Omonia/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
The first line normally gives meaningful information but the rest could easily be muted.
Is there any way to clean this up?

If you're using the standard IRB REPL as rails console, adding the following line :
IRB.conf[:BACK_TRACE_LIMIT] = 1
to your ~/.irbrc file truncates the output to the desired limit.
However I would advise against doing so, because error backtraces can be immensely helpful in complex debugging cases.

Related

RSpec - How to get a better error message?

I am used to PHPUnit, so I found RSpec to be inferior when it comes to showing what has gone wrong, where and why.
For example, in PHPUnit, I can get the stack trace when an exception is raised (and even with -b option in RSpec, all I can get is the stack trace of RSpec exceptions, and not Rails's)
Also, when some error occurs, it shows you the ACTUAL value and the EXPECTED value.
Those two features I'd like to achieve in RSpec. Getting a detailed error message that includes the stack trace, in case of an Exception of Ruby or of Rails, and to know what was the actual value.
Any ideas of how to accomplish this?
If you run
rspec --help
you will see all the options you can pass (or configure via RSpec.configure) to the runner. One of the will force RSpec to show the entire backtrace
-b, --backtrace Enable full backtrace.
You can also configure the excluded/included lines to control how deep you want the backtrace to go.
As for the actuals vs expected value, this is supported by default in RSpec. See for example
For custom-defined objects, it also print out a diff.

Now that ActiveRecord::Base.silence {} is gone, how do I bring back that functionality?

I do a lot of spatial queries that dump massive amounts of text in the form of logs every time I run queries. These slow down my programs enormously.
I'm being forced to update my rails to '4.1.2' from '4.0.0' and ActiveRecord::Base.silence has been completely deprecated as in, it doesn't work. Here's what used to work
ActiveRecord::Base.silence do
noisy_query
end
When I try this now, I get this error....
ArgumentError: wrong number of arguments (0 for 1)
from /Users/davidddouglas/.rvm/gems/ruby-1.9.3-p551/gems/activesupport-4.1.2/lib/active_support/core_ext/kernel/reporting.rb:82:in `capture'
In 4.0.0 it sent a deprecation warning, and now the script just doesn't work. Oddly enough, the function is still declared, it just doesn't work anymore and expects some kind of parameter. I've tried passing in nil and got this error:
NoMethodError: undefined method `reopen' for nil:NilClass
I'm looking for a way to monkeypatch the old functionality back into my program to get my scripts to work again. Not too worried about best practices as this is an application I'm using internally with little to no front end and 0 users other than myself.
Thanks
silence moved to a core extension on logger.
From their example,
logger = Logger.new("log/development.log")
logger.silence(Logger::INFO) do
logger.debug("In space, no one can hear you scream.")
logger.info("Scream all you want, small mailman!")
end

NoMethodError using order intermittently

I am getting the following error in a production rails application intermittently
NoMethodError (undefined method `values' on priority:Symbol.):
app/controllers/things_controller.rb:33:in `index'
The offending line looks like this:
#things = Thing.where(:some_column => 'some_value').order(:priority).reverse
Thing is an activerecord model.
The weird thing is, when I restart the application the error disappears. It is only under some strange set of circumstances that this happens (which I can't reproduce in preprod/dev environments).
Has anyone come across something like this before? Can anyone suggest how I would go about diagnosing this bug? The line in question doesn't seem to be the problem (the logs in production also don't show the full stacktrace)
After a length hair pulling session, I discovered that this is due to a bug in rubinius (2.2.10, and 2.2.9). The ActiveRecord query methods where and order both call enumerable#grep internally. After the application has been running for some time, or some unknown conditions are met, this function stops behaving correctly when the array contains symbols.
When a block is given as an argument to the grep function, this block will always be applied to the symbol elements of the array, regardless of the pattern given.
Bug report is here

puts statements for debug

When I code, I make quite intense use of "puts" statements for debugging. It allows me to see what happens in the server.
When the code is debugged, I use to remove these "puts" statements for I don't know what reason.
Is it a good idea or should I leave them instead to give more clarity to my server logs?
You should use the logger instead of puts. Use this kind of statements:
Rails.logger.debug "DEBUG: #{self.inspect} #{caller(0).first}" if Rails.logger.debug?
If you want to see the debugging in the real-time (almost), just use the tail command in another terminal window:
tail -F log/development.log | grep DEBUG
Then you do not need to remove these statements in production, and they will not degrade performance too much, because if logger.debug? will prevent the (possibly expensive) construction of the message string.
Using standard output for debugging is usually a bad practice. In cases you need such debug, use the diagnostic STDERR, as in:
STDERR.puts "DEBUG: xyzzy"
Most of the classes in Rails (models, controllers and views) have the method logger, so if possible use it instead of the full Rails.logger.
If you are using older versions of Rails, use the constant RAILS_DEFAULT_LOGGER instead of Rails.logger.
use the logger : http://guides.rubyonrails.org/debugging_rails_applications.html#the-logger
I use the rails_dt gem designed specifically to make such kind of debugging easier.
Using Rails.logger or puts directly is somewhat cumbersome, since it requires you to put a lot of decorative stuff (DEBUG, *** etc.) around debug messages to make them different from regular, useful messages.
Also, it's often difficult to find and defuse the debug output generated by Rails.logger or puts if the message doesn't appear to contain enough searchable characters.
rails_dt prints the origin (file, line), so finding the position in code is easy. Also, you will never confuse DT.p with anything, it clearly does debug output and nothing else.
Example:
DT.p "Hello, world!"
# Sent to console, Rails log, dedicated log and Web page, if configured.
[DT app/controllers/root_controller.rb:3] Hello, world!
Gem is available here.

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.

Resources