Topic: How to disable stack trace

I am new to ruby and rails.  When using Rails Console I find the stack trace almost never useful -- and really very much in the way at this point.  For example, I prefer to just be told I have a syntax error and where, without obliterating my output with the trace.  I understand that the trace may be just forwarded from irb, but irb seems to have an option (I think): --stack-trace-limit n", but this option doesn't seem to be recognized by rails console. 

Is there any way to limit or disable the stack trace during rails console?

Last edited by cabu (2010-10-06 03:19:35)

Re: How to disable stack trace

Edit the config/initializers/backtrace_silencers.rb file to remove specific libraries from the output.

More things can go wrong in a program than just a simple syntax error, and that's when full stacktraces become critical. Syntax errors can become a non-issue with syntax checking in TextMate, or things like the Syntastic plugin for Vim.

Last edited by tsigo (2010-10-08 10:58:18)

Re: How to disable stack trace

tsigo: You are right about that -- during my onliine search I found that most people were looking for -more- tracing--but that is not not my case. 

But thanks for the suggestion--I am learning a lot from it.  And so far I haven't solved my problem.:

Here is my config/initializers/backtrace_silencers.rb:
I tried it two ways -- I don't  want any trace
------------------------------------------------------------
Rails.backtrace_cleaner.add_silencer { |line| line =~ /lib/ }
Rails.backtrace_cleaner.add_silencer { |line| true }
-------------------------------------------------------------

Here is my rails console output.  First I typed 'jjj' to get a syntax error
Lots of 'from ...' trace output I don't need here
----------------------------------------------------------------------------
C:\rails_projects\sample_app>rails c
Loading development environment (Rails 3.0.0)
irb(main):002:0> jjj
NameError: undefined local variable or method `jjj' for main:Object
        from (irb):2
        from C:/rails_projects/sample_app/sqlite3-ruby/ruby/1.9.1/gems/railties-3.0.0/lib/rails/commands/console.rb:44:in `start'
        from C:/rails_projects/sample_app/sqlite3-ruby/ruby/1.9.1/gems/railties-3.0.0/lib/rails/commands/console.rb:8:in `start'
[...]
        from script/rails:6:in `<main>'
-------------------------------------------------------------------------------

---so i tried adding a silencer while in the console:
------------------------------------------------------------------------------
irb(main):003:0> Rails.backtrace_cleaner.add_silencer {|line| line =~ /rails_projects/ }
=> [#<Proc:0x3dda840@C:/rails_projects/sample_app/sqlite3-ruby/ruby/1.9.1/gems/railties-3.0.0/lib/rails/backtrace_cleaner.rb:15>, #<Proc:0x3dda810@C:/rails_proj
ects/sample_app/config/initializers/backtrace_silencers.rb:4>, #<Proc:0x3dda7f8@C:/rails_projects/sample_app/config/initializers/backtrace_silencers.rb:5>, #<Proc:0x3847038@(irb):3>]
---------------------------------------------------------------------------------
---I can see the two silencers in my backtrace_silencers.rb file, then the one I just added at (irb):3

---No change in the output
---------------------------------------------------------------------------------
irb(main):004:0> jjj
NameError: undefined local variable or method `jjj' for main:Object
        from (irb):4
        from C:/rails_projects/sample_app/sqlite3-ruby/ruby/1.9.1/gems/railties-3.0.0/lib/rails/commands/console.rb:44:in `start'
        from C:/rails_projects/sample_app/sqlite3-ruby/ruby/1.9.1/gems/railties-3.0.0/lib/rails/commands/console.rb:8:in `start'
        from C:/rails_projects/sample_app/sqlite3-ruby/ruby/1.9.1/gems/railties-3.0.0/lib/rails/commands.rb:23:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
irb(main):005:0>
--------------------------------------------------------------------------------------

What am I doing wrong?  I forgot to say I am using Windows Vista-64 :-(

Re: How to disable stack trace

The solution is to set the following after starting the rails console:

irb>  context.back_trace_limit=0

Re: How to disable stack trace

Thanks cabu. i am just starting to learn ruby and rails and i need all the halp i can get. this sure helped me a lot.



_______________
Blendtec Blender