rails 4.2.11.3 → 5.0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -10
- metadata +38 -244
- data/guides/CHANGELOG.md +0 -113
- data/guides/Rakefile +0 -92
- data/guides/assets/images/akshaysurve.jpg +0 -0
- data/guides/assets/images/belongs_to.png +0 -0
- data/guides/assets/images/book_icon.gif +0 -0
- data/guides/assets/images/bullet.gif +0 -0
- data/guides/assets/images/chapters_icon.gif +0 -0
- data/guides/assets/images/check_bullet.gif +0 -0
- data/guides/assets/images/credits_pic_blank.gif +0 -0
- data/guides/assets/images/csrf.png +0 -0
- data/guides/assets/images/edge_badge.png +0 -0
- data/guides/assets/images/favicon.ico +0 -0
- data/guides/assets/images/feature_tile.gif +0 -0
- data/guides/assets/images/footer_tile.gif +0 -0
- data/guides/assets/images/fxn.png +0 -0
- data/guides/assets/images/getting_started/article_with_comments.png +0 -0
- data/guides/assets/images/getting_started/challenge.png +0 -0
- data/guides/assets/images/getting_started/confirm_dialog.png +0 -0
- data/guides/assets/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
- data/guides/assets/images/getting_started/form_with_errors.png +0 -0
- data/guides/assets/images/getting_started/index_action_with_edit_link.png +0 -0
- data/guides/assets/images/getting_started/new_article.png +0 -0
- data/guides/assets/images/getting_started/rails_welcome.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
- data/guides/assets/images/getting_started/show_action_for_articles.png +0 -0
- data/guides/assets/images/getting_started/template_is_missing_articles_new.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_create_for_articles.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_new_for_articles.png +0 -0
- data/guides/assets/images/grey_bullet.gif +0 -0
- data/guides/assets/images/habtm.png +0 -0
- data/guides/assets/images/has_many.png +0 -0
- data/guides/assets/images/has_many_through.png +0 -0
- data/guides/assets/images/has_one.png +0 -0
- data/guides/assets/images/has_one_through.png +0 -0
- data/guides/assets/images/header_backdrop.png +0 -0
- data/guides/assets/images/header_tile.gif +0 -0
- data/guides/assets/images/i18n/demo_html_safe.png +0 -0
- data/guides/assets/images/i18n/demo_localized_pirate.png +0 -0
- data/guides/assets/images/i18n/demo_translated_en.png +0 -0
- data/guides/assets/images/i18n/demo_translated_pirate.png +0 -0
- data/guides/assets/images/i18n/demo_translation_missing.png +0 -0
- data/guides/assets/images/i18n/demo_untranslated.png +0 -0
- data/guides/assets/images/icons/README +0 -5
- data/guides/assets/images/icons/callouts/1.png +0 -0
- data/guides/assets/images/icons/callouts/10.png +0 -0
- data/guides/assets/images/icons/callouts/11.png +0 -0
- data/guides/assets/images/icons/callouts/12.png +0 -0
- data/guides/assets/images/icons/callouts/13.png +0 -0
- data/guides/assets/images/icons/callouts/14.png +0 -0
- data/guides/assets/images/icons/callouts/15.png +0 -0
- data/guides/assets/images/icons/callouts/2.png +0 -0
- data/guides/assets/images/icons/callouts/3.png +0 -0
- data/guides/assets/images/icons/callouts/4.png +0 -0
- data/guides/assets/images/icons/callouts/5.png +0 -0
- data/guides/assets/images/icons/callouts/6.png +0 -0
- data/guides/assets/images/icons/callouts/7.png +0 -0
- data/guides/assets/images/icons/callouts/8.png +0 -0
- data/guides/assets/images/icons/callouts/9.png +0 -0
- data/guides/assets/images/icons/caution.png +0 -0
- data/guides/assets/images/icons/example.png +0 -0
- data/guides/assets/images/icons/home.png +0 -0
- data/guides/assets/images/icons/important.png +0 -0
- data/guides/assets/images/icons/next.png +0 -0
- data/guides/assets/images/icons/note.png +0 -0
- data/guides/assets/images/icons/prev.png +0 -0
- data/guides/assets/images/icons/tip.png +0 -0
- data/guides/assets/images/icons/up.png +0 -0
- data/guides/assets/images/icons/warning.png +0 -0
- data/guides/assets/images/nav_arrow.gif +0 -0
- data/guides/assets/images/oscardelben.jpg +0 -0
- data/guides/assets/images/polymorphic.png +0 -0
- data/guides/assets/images/radar.png +0 -0
- data/guides/assets/images/rails4_features.png +0 -0
- data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
- data/guides/assets/images/rails_guides_logo.gif +0 -0
- data/guides/assets/images/rails_logo_remix.gif +0 -0
- data/guides/assets/images/session_fixation.png +0 -0
- data/guides/assets/images/tab_grey.gif +0 -0
- data/guides/assets/images/tab_info.gif +0 -0
- data/guides/assets/images/tab_note.gif +0 -0
- data/guides/assets/images/tab_red.gif +0 -0
- data/guides/assets/images/tab_yellow.gif +0 -0
- data/guides/assets/images/tab_yellow.png +0 -0
- data/guides/assets/images/vijaydev.jpg +0 -0
- data/guides/assets/javascripts/guides.js +0 -59
- data/guides/assets/javascripts/jquery.min.js +0 -4
- data/guides/assets/javascripts/responsive-tables.js +0 -43
- data/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
- data/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
- data/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
- data/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
- data/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
- data/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
- data/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
- data/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
- data/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
- data/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
- data/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
- data/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
- data/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
- data/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
- data/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
- data/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
- data/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
- data/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
- data/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
- data/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
- data/guides/assets/javascripts/syntaxhighlighter/shCore.js +0 -17
- data/guides/assets/stylesheets/fixes.css +0 -16
- data/guides/assets/stylesheets/kindle.css +0 -11
- data/guides/assets/stylesheets/main.css +0 -713
- data/guides/assets/stylesheets/print.css +0 -52
- data/guides/assets/stylesheets/reset.css +0 -43
- data/guides/assets/stylesheets/responsive-tables.css +0 -50
- data/guides/assets/stylesheets/style.css +0 -13
- data/guides/assets/stylesheets/syntaxhighlighter/shCore.css +0 -226
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
- data/guides/bug_report_templates/action_controller_gem.rb +0 -47
- data/guides/bug_report_templates/action_controller_master.rb +0 -54
- data/guides/bug_report_templates/active_record_gem.rb +0 -40
- data/guides/bug_report_templates/active_record_master.rb +0 -49
- data/guides/bug_report_templates/generic_gem.rb +0 -15
- data/guides/bug_report_templates/generic_master.rb +0 -26
- data/guides/rails_guides/generator.rb +0 -248
- data/guides/rails_guides/helpers.rb +0 -53
- data/guides/rails_guides/indexer.rb +0 -68
- data/guides/rails_guides/kindle.rb +0 -119
- data/guides/rails_guides/levenshtein.rb +0 -37
- data/guides/rails_guides/markdown/renderer.rb +0 -82
- data/guides/rails_guides/markdown.rb +0 -167
- data/guides/rails_guides.rb +0 -63
- data/guides/source/2_2_release_notes.md +0 -435
- data/guides/source/2_3_release_notes.md +0 -621
- data/guides/source/3_0_release_notes.md +0 -611
- data/guides/source/3_1_release_notes.md +0 -559
- data/guides/source/3_2_release_notes.md +0 -568
- data/guides/source/4_0_release_notes.md +0 -279
- data/guides/source/4_1_release_notes.md +0 -730
- data/guides/source/4_2_release_notes.md +0 -877
- data/guides/source/_license.html.erb +0 -2
- data/guides/source/_welcome.html.erb +0 -23
- data/guides/source/action_controller_overview.md +0 -1192
- data/guides/source/action_mailer_basics.md +0 -757
- data/guides/source/action_view_overview.md +0 -1561
- data/guides/source/active_job_basics.md +0 -339
- data/guides/source/active_model_basics.md +0 -554
- data/guides/source/active_record_basics.md +0 -374
- data/guides/source/active_record_callbacks.md +0 -413
- data/guides/source/active_record_migrations.md +0 -1018
- data/guides/source/active_record_postgresql.md +0 -433
- data/guides/source/active_record_querying.md +0 -1781
- data/guides/source/active_record_validations.md +0 -1179
- data/guides/source/active_support_core_extensions.md +0 -3856
- data/guides/source/active_support_instrumentation.md +0 -488
- data/guides/source/api_documentation_guidelines.md +0 -361
- data/guides/source/asset_pipeline.md +0 -1304
- data/guides/source/association_basics.md +0 -2245
- data/guides/source/autoloading_and_reloading_constants.md +0 -1311
- data/guides/source/caching_with_rails.md +0 -379
- data/guides/source/command_line.md +0 -625
- data/guides/source/configuring.md +0 -1070
- data/guides/source/contributing_to_ruby_on_rails.md +0 -628
- data/guides/source/credits.html.erb +0 -80
- data/guides/source/debugging_rails_applications.md +0 -861
- data/guides/source/development_dependencies_install.md +0 -289
- data/guides/source/documents.yaml +0 -205
- data/guides/source/engines.md +0 -1412
- data/guides/source/form_helpers.md +0 -1024
- data/guides/source/generators.md +0 -676
- data/guides/source/getting_started.md +0 -2086
- data/guides/source/i18n.md +0 -1087
- data/guides/source/index.html.erb +0 -28
- data/guides/source/initialization.md +0 -704
- data/guides/source/kindle/copyright.html.erb +0 -1
- data/guides/source/kindle/layout.html.erb +0 -27
- data/guides/source/kindle/rails_guides.opf.erb +0 -52
- data/guides/source/kindle/toc.html.erb +0 -24
- data/guides/source/kindle/toc.ncx.erb +0 -64
- data/guides/source/kindle/welcome.html.erb +0 -5
- data/guides/source/layout.html.erb +0 -140
- data/guides/source/layouts_and_rendering.md +0 -1226
- data/guides/source/maintenance_policy.md +0 -78
- data/guides/source/nested_model_forms.md +0 -228
- data/guides/source/plugins.md +0 -444
- data/guides/source/rails_application_templates.md +0 -266
- data/guides/source/rails_on_rack.md +0 -335
- data/guides/source/routing.md +0 -1155
- data/guides/source/ruby_on_rails_guides_guidelines.md +0 -127
- data/guides/source/security.md +0 -1024
- data/guides/source/testing.md +0 -1132
- data/guides/source/upgrading_ruby_on_rails.md +0 -1186
- data/guides/source/working_with_javascript_in_rails.md +0 -407
- data/guides/w3c_validator.rb +0 -97
@@ -1,861 +0,0 @@
|
|
1
|
-
Debugging Rails Applications
|
2
|
-
============================
|
3
|
-
|
4
|
-
This guide introduces techniques for debugging Ruby on Rails applications.
|
5
|
-
|
6
|
-
After reading this guide, you will know:
|
7
|
-
|
8
|
-
* The purpose of debugging.
|
9
|
-
* How to track down problems and issues in your application that your tests aren't identifying.
|
10
|
-
* The different ways of debugging.
|
11
|
-
* How to analyze the stack trace.
|
12
|
-
|
13
|
-
--------------------------------------------------------------------------------
|
14
|
-
|
15
|
-
View Helpers for Debugging
|
16
|
-
--------------------------
|
17
|
-
|
18
|
-
One common task is to inspect the contents of a variable. In Rails, you can do this with three methods:
|
19
|
-
|
20
|
-
* `debug`
|
21
|
-
* `to_yaml`
|
22
|
-
* `inspect`
|
23
|
-
|
24
|
-
### `debug`
|
25
|
-
|
26
|
-
The `debug` helper will return a \<pre> tag that renders the object using the YAML format. This will generate human-readable data from any object. For example, if you have this code in a view:
|
27
|
-
|
28
|
-
```html+erb
|
29
|
-
<%= debug @article %>
|
30
|
-
<p>
|
31
|
-
<b>Title:</b>
|
32
|
-
<%= @article.title %>
|
33
|
-
</p>
|
34
|
-
```
|
35
|
-
|
36
|
-
You'll see something like this:
|
37
|
-
|
38
|
-
```yaml
|
39
|
-
--- !ruby/object Article
|
40
|
-
attributes:
|
41
|
-
updated_at: 2008-09-05 22:55:47
|
42
|
-
body: It's a very helpful guide for debugging your Rails app.
|
43
|
-
title: Rails debugging guide
|
44
|
-
published: t
|
45
|
-
id: "1"
|
46
|
-
created_at: 2008-09-05 22:55:47
|
47
|
-
attributes_cache: {}
|
48
|
-
|
49
|
-
|
50
|
-
Title: Rails debugging guide
|
51
|
-
```
|
52
|
-
|
53
|
-
### `to_yaml`
|
54
|
-
|
55
|
-
Displaying an instance variable, or any other object or method, in YAML format can be achieved this way:
|
56
|
-
|
57
|
-
```html+erb
|
58
|
-
<%= simple_format @article.to_yaml %>
|
59
|
-
<p>
|
60
|
-
<b>Title:</b>
|
61
|
-
<%= @article.title %>
|
62
|
-
</p>
|
63
|
-
```
|
64
|
-
|
65
|
-
The `to_yaml` method converts the method to YAML format leaving it more readable, and then the `simple_format` helper is used to render each line as in the console. This is how `debug` method does its magic.
|
66
|
-
|
67
|
-
As a result of this, you will have something like this in your view:
|
68
|
-
|
69
|
-
```yaml
|
70
|
-
--- !ruby/object Article
|
71
|
-
attributes:
|
72
|
-
updated_at: 2008-09-05 22:55:47
|
73
|
-
body: It's a very helpful guide for debugging your Rails app.
|
74
|
-
title: Rails debugging guide
|
75
|
-
published: t
|
76
|
-
id: "1"
|
77
|
-
created_at: 2008-09-05 22:55:47
|
78
|
-
attributes_cache: {}
|
79
|
-
|
80
|
-
Title: Rails debugging guide
|
81
|
-
```
|
82
|
-
|
83
|
-
### `inspect`
|
84
|
-
|
85
|
-
Another useful method for displaying object values is `inspect`, especially when working with arrays or hashes. This will print the object value as a string. For example:
|
86
|
-
|
87
|
-
```html+erb
|
88
|
-
<%= [1, 2, 3, 4, 5].inspect %>
|
89
|
-
<p>
|
90
|
-
<b>Title:</b>
|
91
|
-
<%= @article.title %>
|
92
|
-
</p>
|
93
|
-
```
|
94
|
-
|
95
|
-
Will be rendered as follows:
|
96
|
-
|
97
|
-
```
|
98
|
-
[1, 2, 3, 4, 5]
|
99
|
-
|
100
|
-
Title: Rails debugging guide
|
101
|
-
```
|
102
|
-
|
103
|
-
The Logger
|
104
|
-
----------
|
105
|
-
|
106
|
-
It can also be useful to save information to log files at runtime. Rails maintains a separate log file for each runtime environment.
|
107
|
-
|
108
|
-
### What is the Logger?
|
109
|
-
|
110
|
-
Rails makes use of the `ActiveSupport::Logger` class to write log information. You can also substitute another logger such as `Log4r` if you wish.
|
111
|
-
|
112
|
-
You can specify an alternative logger in your `environment.rb` or any environment file:
|
113
|
-
|
114
|
-
```ruby
|
115
|
-
Rails.logger = Logger.new(STDOUT)
|
116
|
-
Rails.logger = Log4r::Logger.new("Application Log")
|
117
|
-
```
|
118
|
-
|
119
|
-
Or in the `Initializer` section, add _any_ of the following
|
120
|
-
|
121
|
-
```ruby
|
122
|
-
config.logger = Logger.new(STDOUT)
|
123
|
-
config.logger = Log4r::Logger.new("Application Log")
|
124
|
-
```
|
125
|
-
|
126
|
-
TIP: By default, each log is created under `Rails.root/log/` and the log file is named after the environment in which the application is running.
|
127
|
-
|
128
|
-
### Log Levels
|
129
|
-
|
130
|
-
When something is logged it's printed into the corresponding log if the log level of the message is equal or higher than the configured log level. If you want to know the current log level you can call the `Rails.logger.level` method.
|
131
|
-
|
132
|
-
The available log levels are: `:debug`, `:info`, `:warn`, `:error`, `:fatal`, and `:unknown`, corresponding to the log level numbers from 0 up to 5 respectively. To change the default log level, use
|
133
|
-
|
134
|
-
```ruby
|
135
|
-
config.log_level = :warn # In any environment initializer, or
|
136
|
-
Rails.logger.level = 0 # at any time
|
137
|
-
```
|
138
|
-
|
139
|
-
This is useful when you want to log under development or staging, but you don't want to flood your production log with unnecessary information.
|
140
|
-
|
141
|
-
TIP: The default Rails log level is `debug` in all environments.
|
142
|
-
|
143
|
-
### Sending Messages
|
144
|
-
|
145
|
-
To write in the current log use the `logger.(debug|info|warn|error|fatal)` method from within a controller, model or mailer:
|
146
|
-
|
147
|
-
```ruby
|
148
|
-
logger.debug "Person attributes hash: #{@person.attributes.inspect}"
|
149
|
-
logger.info "Processing the request..."
|
150
|
-
logger.fatal "Terminating application, raised unrecoverable error!!!"
|
151
|
-
```
|
152
|
-
|
153
|
-
Here's an example of a method instrumented with extra logging:
|
154
|
-
|
155
|
-
```ruby
|
156
|
-
class ArticlesController < ApplicationController
|
157
|
-
# ...
|
158
|
-
|
159
|
-
def create
|
160
|
-
@article = Article.new(params[:article])
|
161
|
-
logger.debug "New article: #{@article.attributes.inspect}"
|
162
|
-
logger.debug "Article should be valid: #{@article.valid?}"
|
163
|
-
|
164
|
-
if @article.save
|
165
|
-
flash[:notice] = 'Article was successfully created.'
|
166
|
-
logger.debug "The article was saved and now the user is going to be redirected..."
|
167
|
-
redirect_to(@article)
|
168
|
-
else
|
169
|
-
render action: "new"
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
# ...
|
174
|
-
end
|
175
|
-
```
|
176
|
-
|
177
|
-
Here's an example of the log generated when this controller action is executed:
|
178
|
-
|
179
|
-
```
|
180
|
-
Processing ArticlesController#create (for 127.0.0.1 at 2008-09-08 11:52:54) [POST]
|
181
|
-
Session ID: BAh7BzoMY3NyZl9pZCIlMDY5MWU1M2I1ZDRjODBlMzkyMWI1OTg2NWQyNzViZjYiCmZsYXNoSUM6J0FjdGl
|
182
|
-
vbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7AAY6CkB1c2VkewA=--b18cd92fba90eacf8137e5f6b3b06c4d724596a4
|
183
|
-
Parameters: {"commit"=>"Create", "article"=>{"title"=>"Debugging Rails",
|
184
|
-
"body"=>"I'm learning how to print in logs!!!", "published"=>"0"},
|
185
|
-
"authenticity_token"=>"2059c1286e93402e389127b1153204e0d1e275dd", "action"=>"create", "controller"=>"articles"}
|
186
|
-
New article: {"updated_at"=>nil, "title"=>"Debugging Rails", "body"=>"I'm learning how to print in logs!!!",
|
187
|
-
"published"=>false, "created_at"=>nil}
|
188
|
-
Article should be valid: true
|
189
|
-
Article Create (0.000443) INSERT INTO "articles" ("updated_at", "title", "body", "published",
|
190
|
-
"created_at") VALUES('2008-09-08 14:52:54', 'Debugging Rails',
|
191
|
-
'I''m learning how to print in logs!!!', 'f', '2008-09-08 14:52:54')
|
192
|
-
The article was saved and now the user is going to be redirected...
|
193
|
-
Redirected to # Article:0x20af760>
|
194
|
-
Completed in 0.01224 (81 reqs/sec) | DB: 0.00044 (3%) | 302 Found [http://localhost/articles]
|
195
|
-
```
|
196
|
-
|
197
|
-
Adding extra logging like this makes it easy to search for unexpected or unusual behavior in your logs. If you add extra logging, be sure to make sensible use of log levels to avoid filling your production logs with useless trivia.
|
198
|
-
|
199
|
-
### Tagged Logging
|
200
|
-
|
201
|
-
When running multi-user, multi-account applications, it's often useful
|
202
|
-
to be able to filter the logs using some custom rules. `TaggedLogging`
|
203
|
-
in Active Support helps in doing exactly that by stamping log lines with subdomains, request ids, and anything else to aid debugging such applications.
|
204
|
-
|
205
|
-
```ruby
|
206
|
-
logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
|
207
|
-
logger.tagged("BCX") { logger.info "Stuff" } # Logs "[BCX] Stuff"
|
208
|
-
logger.tagged("BCX", "Jason") { logger.info "Stuff" } # Logs "[BCX] [Jason] Stuff"
|
209
|
-
logger.tagged("BCX") { logger.tagged("Jason") { logger.info "Stuff" } } # Logs "[BCX] [Jason] Stuff"
|
210
|
-
```
|
211
|
-
|
212
|
-
### Impact of Logs on Performance
|
213
|
-
Logging will always have a small impact on performance of your rails app,
|
214
|
-
particularly when logging to disk. However, there are a few subtleties:
|
215
|
-
|
216
|
-
Using the `:debug` level will have a greater performance penalty than `:fatal`,
|
217
|
-
as a far greater number of strings are being evaluated and written to the
|
218
|
-
log output (e.g. disk).
|
219
|
-
|
220
|
-
Another potential pitfall is that if you have many calls to `Logger` like this
|
221
|
-
in your code:
|
222
|
-
|
223
|
-
```ruby
|
224
|
-
logger.debug "Person attributes hash: #{@person.attributes.inspect}"
|
225
|
-
```
|
226
|
-
|
227
|
-
In the above example, There will be a performance impact even if the allowed
|
228
|
-
output level doesn't include debug. The reason is that Ruby has to evaluate
|
229
|
-
these strings, which includes instantiating the somewhat heavy `String` object
|
230
|
-
and interpolating the variables, and which takes time.
|
231
|
-
Therefore, it's recommended to pass blocks to the logger methods, as these are
|
232
|
-
only evaluated if the output level is the same or included in the allowed level
|
233
|
-
(i.e. lazy loading). The same code rewritten would be:
|
234
|
-
|
235
|
-
```ruby
|
236
|
-
logger.debug {"Person attributes hash: #{@person.attributes.inspect}"}
|
237
|
-
```
|
238
|
-
|
239
|
-
The contents of the block, and therefore the string interpolation, is only
|
240
|
-
evaluated if debug is enabled. This performance savings is only really
|
241
|
-
noticeable with large amounts of logging, but it's a good practice to employ.
|
242
|
-
|
243
|
-
Debugging with the `byebug` gem
|
244
|
-
---------------------------------
|
245
|
-
|
246
|
-
When your code is behaving in unexpected ways, you can try printing to logs or
|
247
|
-
the console to diagnose the problem. Unfortunately, there are times when this
|
248
|
-
sort of error tracking is not effective in finding the root cause of a problem.
|
249
|
-
When you actually need to journey into your running source code, the debugger
|
250
|
-
is your best companion.
|
251
|
-
|
252
|
-
The debugger can also help you if you want to learn about the Rails source code
|
253
|
-
but don't know where to start. Just debug any request to your application and
|
254
|
-
use this guide to learn how to move from the code you have written deeper into
|
255
|
-
Rails code.
|
256
|
-
|
257
|
-
### Setup
|
258
|
-
|
259
|
-
You can use the `byebug` gem to set breakpoints and step through live code in
|
260
|
-
Rails. To install it, just run:
|
261
|
-
|
262
|
-
```bash
|
263
|
-
$ gem install byebug
|
264
|
-
```
|
265
|
-
|
266
|
-
Inside any Rails application you can then invoke the debugger by calling the
|
267
|
-
`byebug` method.
|
268
|
-
|
269
|
-
Here's an example:
|
270
|
-
|
271
|
-
```ruby
|
272
|
-
class PeopleController < ApplicationController
|
273
|
-
def new
|
274
|
-
byebug
|
275
|
-
@person = Person.new
|
276
|
-
end
|
277
|
-
end
|
278
|
-
```
|
279
|
-
|
280
|
-
### The Shell
|
281
|
-
|
282
|
-
As soon as your application calls the `byebug` method, the debugger will be
|
283
|
-
started in a debugger shell inside the terminal window where you launched your
|
284
|
-
application server, and you will be placed at the debugger's prompt `(byebug)`.
|
285
|
-
Before the prompt, the code around the line that is about to be run will be
|
286
|
-
displayed and the current line will be marked by '=>'. Like this:
|
287
|
-
|
288
|
-
```
|
289
|
-
[1, 10] in /PathTo/project/app/controllers/articles_controller.rb
|
290
|
-
3:
|
291
|
-
4: # GET /articles
|
292
|
-
5: # GET /articles.json
|
293
|
-
6: def index
|
294
|
-
7: byebug
|
295
|
-
=> 8: @articles = Article.find_recent
|
296
|
-
9:
|
297
|
-
10: respond_to do |format|
|
298
|
-
11: format.html # index.html.erb
|
299
|
-
12: format.json { render json: @articles }
|
300
|
-
|
301
|
-
(byebug)
|
302
|
-
```
|
303
|
-
|
304
|
-
If you got there by a browser request, the browser tab containing the request
|
305
|
-
will be hung until the debugger has finished and the trace has finished
|
306
|
-
processing the entire request.
|
307
|
-
|
308
|
-
For example:
|
309
|
-
|
310
|
-
```bash
|
311
|
-
=> Booting WEBrick
|
312
|
-
=> Rails 4.2.0 application starting in development on http://0.0.0.0:3000
|
313
|
-
=> Run `rails server -h` for more startup options
|
314
|
-
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
|
315
|
-
=> Ctrl-C to shutdown server
|
316
|
-
[2014-04-11 13:11:47] INFO WEBrick 1.3.1
|
317
|
-
[2014-04-11 13:11:47] INFO ruby 2.1.1 (2014-02-24) [i686-linux]
|
318
|
-
[2014-04-11 13:11:47] INFO WEBrick::HTTPServer#start: pid=6370 port=3000
|
319
|
-
|
320
|
-
|
321
|
-
Started GET "/" for 127.0.0.1 at 2014-04-11 13:11:48 +0200
|
322
|
-
ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
|
323
|
-
Processing by ArticlesController#index as HTML
|
324
|
-
|
325
|
-
[3, 12] in /PathTo/project/app/controllers/articles_controller.rb
|
326
|
-
3:
|
327
|
-
4: # GET /articles
|
328
|
-
5: # GET /articles.json
|
329
|
-
6: def index
|
330
|
-
7: byebug
|
331
|
-
=> 8: @articles = Article.find_recent
|
332
|
-
9:
|
333
|
-
10: respond_to do |format|
|
334
|
-
11: format.html # index.html.erb
|
335
|
-
12: format.json { render json: @articles }
|
336
|
-
|
337
|
-
(byebug)
|
338
|
-
```
|
339
|
-
|
340
|
-
Now it's time to explore and dig into your application. A good place to start is
|
341
|
-
by asking the debugger for help. Type: `help`
|
342
|
-
|
343
|
-
```
|
344
|
-
(byebug) help
|
345
|
-
|
346
|
-
byebug 2.7.0
|
347
|
-
|
348
|
-
Type 'help <command-name>' for help on a specific command
|
349
|
-
|
350
|
-
Available commands:
|
351
|
-
backtrace delete enable help list pry next restart source up
|
352
|
-
break disable eval info method ps save step var
|
353
|
-
catch display exit interrupt next putl set thread
|
354
|
-
condition down finish irb p quit show trace
|
355
|
-
continue edit frame kill pp reload skip undisplay
|
356
|
-
```
|
357
|
-
|
358
|
-
TIP: To view the help menu for any command use `help <command-name>` at the
|
359
|
-
debugger prompt. For example: _`help list`_. You can abbreviate any debugging
|
360
|
-
command by supplying just enough letters to distinguish them from other
|
361
|
-
commands, so you can also use `l` for the `list` command, for example.
|
362
|
-
|
363
|
-
To see the previous ten lines you should type `list-` (or `l-`)
|
364
|
-
|
365
|
-
```
|
366
|
-
(byebug) l-
|
367
|
-
|
368
|
-
[1, 10] in /PathTo/project/app/controllers/articles_controller.rb
|
369
|
-
1 class ArticlesController < ApplicationController
|
370
|
-
2 before_action :set_article, only: [:show, :edit, :update, :destroy]
|
371
|
-
3
|
372
|
-
4 # GET /articles
|
373
|
-
5 # GET /articles.json
|
374
|
-
6 def index
|
375
|
-
7 byebug
|
376
|
-
8 @articles = Article.find_recent
|
377
|
-
9
|
378
|
-
10 respond_to do |format|
|
379
|
-
|
380
|
-
```
|
381
|
-
|
382
|
-
This way you can move inside the file, being able to see the code above and over
|
383
|
-
the line where you added the `byebug` call. Finally, to see where you are in
|
384
|
-
the code again you can type `list=`
|
385
|
-
|
386
|
-
```
|
387
|
-
(byebug) list=
|
388
|
-
|
389
|
-
[3, 12] in /PathTo/project/app/controllers/articles_controller.rb
|
390
|
-
3:
|
391
|
-
4: # GET /articles
|
392
|
-
5: # GET /articles.json
|
393
|
-
6: def index
|
394
|
-
7: byebug
|
395
|
-
=> 8: @articles = Article.find_recent
|
396
|
-
9:
|
397
|
-
10: respond_to do |format|
|
398
|
-
11: format.html # index.html.erb
|
399
|
-
12: format.json { render json: @articles }
|
400
|
-
|
401
|
-
(byebug)
|
402
|
-
```
|
403
|
-
|
404
|
-
### The Context
|
405
|
-
|
406
|
-
When you start debugging your application, you will be placed in different
|
407
|
-
contexts as you go through the different parts of the stack.
|
408
|
-
|
409
|
-
The debugger creates a context when a stopping point or an event is reached. The
|
410
|
-
context has information about the suspended program which enables the debugger
|
411
|
-
to inspect the frame stack, evaluate variables from the perspective of the
|
412
|
-
debugged program, and contains information about the place where the debugged
|
413
|
-
program is stopped.
|
414
|
-
|
415
|
-
At any time you can call the `backtrace` command (or its alias `where`) to print
|
416
|
-
the backtrace of the application. This can be very helpful to know how you got
|
417
|
-
where you are. If you ever wondered about how you got somewhere in your code,
|
418
|
-
then `backtrace` will supply the answer.
|
419
|
-
|
420
|
-
```
|
421
|
-
(byebug) where
|
422
|
-
--> #0 ArticlesController.index
|
423
|
-
at /PathTo/project/test_app/app/controllers/articles_controller.rb:8
|
424
|
-
#1 ActionController::ImplicitRender.send_action(method#String, *args#Array)
|
425
|
-
at /PathToGems/actionpack-4.2.0/lib/action_controller/metal/implicit_render.rb:4
|
426
|
-
#2 AbstractController::Base.process_action(action#NilClass, *args#Array)
|
427
|
-
at /PathToGems/actionpack-4.2.0/lib/abstract_controller/base.rb:189
|
428
|
-
#3 ActionController::Rendering.process_action(action#NilClass, *args#NilClass)
|
429
|
-
at /PathToGems/actionpack-4.2.0/lib/action_controller/metal/rendering.rb:10
|
430
|
-
...
|
431
|
-
```
|
432
|
-
|
433
|
-
The current frame is marked with `-->`. You can move anywhere you want in this
|
434
|
-
trace (thus changing the context) by using the `frame _n_` command, where _n_ is
|
435
|
-
the specified frame number. If you do that, `byebug` will display your new
|
436
|
-
context.
|
437
|
-
|
438
|
-
```
|
439
|
-
(byebug) frame 2
|
440
|
-
|
441
|
-
[184, 193] in /PathToGems/actionpack-4.2.0/lib/abstract_controller/base.rb
|
442
|
-
184: # is the intended way to override action dispatching.
|
443
|
-
185: #
|
444
|
-
186: # Notice that the first argument is the method to be dispatched
|
445
|
-
187: # which is *not* necessarily the same as the action name.
|
446
|
-
188: def process_action(method_name, *args)
|
447
|
-
=> 189: send_action(method_name, *args)
|
448
|
-
190: end
|
449
|
-
191:
|
450
|
-
192: # Actually call the method associated with the action. Override
|
451
|
-
193: # this method if you wish to change how action methods are called,
|
452
|
-
|
453
|
-
(byebug)
|
454
|
-
```
|
455
|
-
|
456
|
-
The available variables are the same as if you were running the code line by
|
457
|
-
line. After all, that's what debugging is.
|
458
|
-
|
459
|
-
You can also use `up [n]` (`u` for abbreviated) and `down [n]` commands in order
|
460
|
-
to change the context _n_ frames up or down the stack respectively. _n_ defaults
|
461
|
-
to one. Up in this case is towards higher-numbered stack frames, and down is
|
462
|
-
towards lower-numbered stack frames.
|
463
|
-
|
464
|
-
### Threads
|
465
|
-
|
466
|
-
The debugger can list, stop, resume and switch between running threads by using
|
467
|
-
the `thread` command (or the abbreviated `th`). This command has a handful of
|
468
|
-
options:
|
469
|
-
|
470
|
-
* `thread` shows the current thread.
|
471
|
-
* `thread list` is used to list all threads and their statuses. The plus +
|
472
|
-
character and the number indicates the current thread of execution.
|
473
|
-
* `thread stop _n_` stop thread _n_.
|
474
|
-
* `thread resume _n_` resumes thread _n_.
|
475
|
-
* `thread switch _n_` switches the current thread context to _n_.
|
476
|
-
|
477
|
-
This command is very helpful, among other occasions, when you are debugging
|
478
|
-
concurrent threads and need to verify that there are no race conditions in your
|
479
|
-
code.
|
480
|
-
|
481
|
-
### Inspecting Variables
|
482
|
-
|
483
|
-
Any expression can be evaluated in the current context. To evaluate an
|
484
|
-
expression, just type it!
|
485
|
-
|
486
|
-
This example shows how you can print the instance variables defined within the
|
487
|
-
current context:
|
488
|
-
|
489
|
-
```
|
490
|
-
[3, 12] in /PathTo/project/app/controllers/articles_controller.rb
|
491
|
-
3:
|
492
|
-
4: # GET /articles
|
493
|
-
5: # GET /articles.json
|
494
|
-
6: def index
|
495
|
-
7: byebug
|
496
|
-
=> 8: @articles = Article.find_recent
|
497
|
-
9:
|
498
|
-
10: respond_to do |format|
|
499
|
-
11: format.html # index.html.erb
|
500
|
-
12: format.json { render json: @articles }
|
501
|
-
|
502
|
-
(byebug) instance_variables
|
503
|
-
[:@_action_has_layout, :@_routes, :@_headers, :@_status, :@_request,
|
504
|
-
:@_response, :@_env, :@_prefixes, :@_lookup_context, :@_action_name,
|
505
|
-
:@_response_body, :@marked_for_same_origin_verification, :@_config]
|
506
|
-
```
|
507
|
-
|
508
|
-
As you may have figured out, all of the variables that you can access from a
|
509
|
-
controller are displayed. This list is dynamically updated as you execute code.
|
510
|
-
For example, run the next line using `next` (you'll learn more about this
|
511
|
-
command later in this guide).
|
512
|
-
|
513
|
-
```
|
514
|
-
(byebug) next
|
515
|
-
[5, 14] in /PathTo/project/app/controllers/articles_controller.rb
|
516
|
-
5 # GET /articles.json
|
517
|
-
6 def index
|
518
|
-
7 byebug
|
519
|
-
8 @articles = Article.find_recent
|
520
|
-
9
|
521
|
-
=> 10 respond_to do |format|
|
522
|
-
11 format.html # index.html.erb
|
523
|
-
12 format.json { render json: @articles }
|
524
|
-
13 end
|
525
|
-
14 end
|
526
|
-
15
|
527
|
-
(byebug)
|
528
|
-
```
|
529
|
-
|
530
|
-
And then ask again for the instance_variables:
|
531
|
-
|
532
|
-
```
|
533
|
-
(byebug) instance_variables.include? "@articles"
|
534
|
-
true
|
535
|
-
```
|
536
|
-
|
537
|
-
Now `@articles` is included in the instance variables, because the line defining it
|
538
|
-
was executed.
|
539
|
-
|
540
|
-
TIP: You can also step into **irb** mode with the command `irb` (of course!).
|
541
|
-
This way an irb session will be started within the context you invoked it. But
|
542
|
-
be warned: this is an experimental feature.
|
543
|
-
|
544
|
-
The `var` method is the most convenient way to show variables and their values.
|
545
|
-
Let's let `byebug` to help us with it.
|
546
|
-
|
547
|
-
```
|
548
|
-
(byebug) help var
|
549
|
-
v[ar] cl[ass] show class variables of self
|
550
|
-
v[ar] const <object> show constants of object
|
551
|
-
v[ar] g[lobal] show global variables
|
552
|
-
v[ar] i[nstance] <object> show instance variables of object
|
553
|
-
v[ar] l[ocal] show local variables
|
554
|
-
```
|
555
|
-
|
556
|
-
This is a great way to inspect the values of the current context variables. For
|
557
|
-
example, to check that we have no local variables currently defined.
|
558
|
-
|
559
|
-
```
|
560
|
-
(byebug) var local
|
561
|
-
(byebug)
|
562
|
-
```
|
563
|
-
|
564
|
-
You can also inspect for an object method this way:
|
565
|
-
|
566
|
-
```
|
567
|
-
(byebug) var instance Article.new
|
568
|
-
@_start_transaction_state = {}
|
569
|
-
@aggregation_cache = {}
|
570
|
-
@association_cache = {}
|
571
|
-
@attributes = {"id"=>nil, "created_at"=>nil, "updated_at"=>nil}
|
572
|
-
@attributes_cache = {}
|
573
|
-
@changed_attributes = nil
|
574
|
-
...
|
575
|
-
```
|
576
|
-
|
577
|
-
TIP: The commands `p` (print) and `pp` (pretty print) can be used to evaluate
|
578
|
-
Ruby expressions and display the value of variables to the console.
|
579
|
-
|
580
|
-
You can use also `display` to start watching variables. This is a good way of
|
581
|
-
tracking the values of a variable while the execution goes on.
|
582
|
-
|
583
|
-
```
|
584
|
-
(byebug) display @articles
|
585
|
-
1: @articles = nil
|
586
|
-
```
|
587
|
-
|
588
|
-
The variables inside the displaying list will be printed with their values after
|
589
|
-
you move in the stack. To stop displaying a variable use `undisplay _n_` where
|
590
|
-
_n_ is the variable number (1 in the last example).
|
591
|
-
|
592
|
-
### Step by Step
|
593
|
-
|
594
|
-
Now you should know where you are in the running trace and be able to print the
|
595
|
-
available variables. But lets continue and move on with the application
|
596
|
-
execution.
|
597
|
-
|
598
|
-
Use `step` (abbreviated `s`) to continue running your program until the next
|
599
|
-
logical stopping point and return control to the debugger.
|
600
|
-
|
601
|
-
You may also use `next` which is similar to step, but function or method calls
|
602
|
-
that appear within the line of code are executed without stopping.
|
603
|
-
|
604
|
-
TIP: You can also use `step n` or `next n` to move forwards `n` steps at once.
|
605
|
-
|
606
|
-
The difference between `next` and `step` is that `step` stops at the next line
|
607
|
-
of code executed, doing just a single step, while `next` moves to the next line
|
608
|
-
without descending inside methods.
|
609
|
-
|
610
|
-
For example, consider the following situation:
|
611
|
-
|
612
|
-
```ruby
|
613
|
-
Started GET "/" for 127.0.0.1 at 2014-04-11 13:39:23 +0200
|
614
|
-
Processing by ArticlesController#index as HTML
|
615
|
-
|
616
|
-
[1, 8] in /home/davidr/Proyectos/test_app/app/models/article.rb
|
617
|
-
1: class Article < ActiveRecord::Base
|
618
|
-
2:
|
619
|
-
3: def self.find_recent(limit = 10)
|
620
|
-
4: byebug
|
621
|
-
=> 5: where('created_at > ?', 1.week.ago).limit(limit)
|
622
|
-
6: end
|
623
|
-
7:
|
624
|
-
8: end
|
625
|
-
|
626
|
-
(byebug)
|
627
|
-
```
|
628
|
-
|
629
|
-
If we use `next`, we want go deep inside method calls. Instead, byebug will go
|
630
|
-
to the next line within the same context. In this case, this is the last line of
|
631
|
-
the method, so `byebug` will jump to next next line of the previous frame.
|
632
|
-
|
633
|
-
```
|
634
|
-
(byebug) next
|
635
|
-
Next went up a frame because previous frame finished
|
636
|
-
|
637
|
-
[4, 13] in /PathTo/project/test_app/app/controllers/articles_controller.rb
|
638
|
-
4: # GET /articles
|
639
|
-
5: # GET /articles.json
|
640
|
-
6: def index
|
641
|
-
7: @articles = Article.find_recent
|
642
|
-
8:
|
643
|
-
=> 9: respond_to do |format|
|
644
|
-
10: format.html # index.html.erb
|
645
|
-
11: format.json { render json: @articles }
|
646
|
-
12: end
|
647
|
-
13: end
|
648
|
-
|
649
|
-
(byebug)
|
650
|
-
```
|
651
|
-
|
652
|
-
If we use `step` in the same situation, we will literally go the next ruby
|
653
|
-
instruction to be executed. In this case, the activesupport's `week` method.
|
654
|
-
|
655
|
-
```
|
656
|
-
(byebug) step
|
657
|
-
|
658
|
-
[50, 59] in /PathToGems/activesupport-4.2.0/lib/active_support/core_ext/numeric/time.rb
|
659
|
-
50: ActiveSupport::Duration.new(self * 24.hours, [[:days, self]])
|
660
|
-
51: end
|
661
|
-
52: alias :day :days
|
662
|
-
53:
|
663
|
-
54: def weeks
|
664
|
-
=> 55: ActiveSupport::Duration.new(self * 7.days, [[:days, self * 7]])
|
665
|
-
56: end
|
666
|
-
57: alias :week :weeks
|
667
|
-
58:
|
668
|
-
59: def fortnights
|
669
|
-
|
670
|
-
(byebug)
|
671
|
-
```
|
672
|
-
|
673
|
-
This is one of the best ways to find bugs in your code, or perhaps in Ruby on
|
674
|
-
Rails.
|
675
|
-
|
676
|
-
### Breakpoints
|
677
|
-
|
678
|
-
A breakpoint makes your application stop whenever a certain point in the program
|
679
|
-
is reached. The debugger shell is invoked in that line.
|
680
|
-
|
681
|
-
You can add breakpoints dynamically with the command `break` (or just `b`).
|
682
|
-
There are 3 possible ways of adding breakpoints manually:
|
683
|
-
|
684
|
-
* `break line`: set breakpoint in the _line_ in the current source file.
|
685
|
-
* `break file:line [if expression]`: set breakpoint in the _line_ number inside
|
686
|
-
the _file_. If an _expression_ is given it must evaluated to _true_ to fire up
|
687
|
-
the debugger.
|
688
|
-
* `break class(.|\#)method [if expression]`: set breakpoint in _method_ (. and
|
689
|
-
\# for class and instance method respectively) defined in _class_. The
|
690
|
-
_expression_ works the same way as with file:line.
|
691
|
-
|
692
|
-
|
693
|
-
For example, in the previous situation
|
694
|
-
|
695
|
-
```
|
696
|
-
[4, 13] in /PathTo/project/app/controllers/articles_controller.rb
|
697
|
-
4: # GET /articles
|
698
|
-
5: # GET /articles.json
|
699
|
-
6: def index
|
700
|
-
7: @articles = Article.find_recent
|
701
|
-
8:
|
702
|
-
=> 9: respond_to do |format|
|
703
|
-
10: format.html # index.html.erb
|
704
|
-
11: format.json { render json: @articles }
|
705
|
-
12: end
|
706
|
-
13: end
|
707
|
-
|
708
|
-
(byebug) break 11
|
709
|
-
Created breakpoint 1 at /PathTo/project/app/controllers/articles_controller.rb:11
|
710
|
-
|
711
|
-
```
|
712
|
-
|
713
|
-
Use `info breakpoints _n_` or `info break _n_` to list breakpoints. If you
|
714
|
-
supply a number, it lists that breakpoint. Otherwise it lists all breakpoints.
|
715
|
-
|
716
|
-
```
|
717
|
-
(byebug) info breakpoints
|
718
|
-
Num Enb What
|
719
|
-
1 y at /PathTo/project/app/controllers/articles_controller.rb:11
|
720
|
-
```
|
721
|
-
|
722
|
-
To delete breakpoints: use the command `delete _n_` to remove the breakpoint
|
723
|
-
number _n_. If no number is specified, it deletes all breakpoints that are
|
724
|
-
currently active.
|
725
|
-
|
726
|
-
```
|
727
|
-
(byebug) delete 1
|
728
|
-
(byebug) info breakpoints
|
729
|
-
No breakpoints.
|
730
|
-
```
|
731
|
-
|
732
|
-
You can also enable or disable breakpoints:
|
733
|
-
|
734
|
-
* `enable breakpoints`: allow a _breakpoints_ list or all of them if no list is
|
735
|
-
specified, to stop your program. This is the default state when you create a
|
736
|
-
breakpoint.
|
737
|
-
* `disable breakpoints`: the _breakpoints_ will have no effect on your program.
|
738
|
-
|
739
|
-
### Catching Exceptions
|
740
|
-
|
741
|
-
The command `catch exception-name` (or just `cat exception-name`) can be used to
|
742
|
-
intercept an exception of type _exception-name_ when there would otherwise be no
|
743
|
-
handler for it.
|
744
|
-
|
745
|
-
To list all active catchpoints use `catch`.
|
746
|
-
|
747
|
-
### Resuming Execution
|
748
|
-
|
749
|
-
There are two ways to resume execution of an application that is stopped in the
|
750
|
-
debugger:
|
751
|
-
|
752
|
-
* `continue` [line-specification] \(or `c`): resume program execution, at the
|
753
|
-
address where your script last stopped; any breakpoints set at that address are
|
754
|
-
bypassed. The optional argument line-specification allows you to specify a line
|
755
|
-
number to set a one-time breakpoint which is deleted when that breakpoint is
|
756
|
-
reached.
|
757
|
-
* `finish` [frame-number] \(or `fin`): execute until the selected stack frame
|
758
|
-
returns. If no frame number is given, the application will run until the
|
759
|
-
currently selected frame returns. The currently selected frame starts out the
|
760
|
-
most-recent frame or 0 if no frame positioning (e.g up, down or frame) has been
|
761
|
-
performed. If a frame number is given it will run until the specified frame
|
762
|
-
returns.
|
763
|
-
|
764
|
-
### Editing
|
765
|
-
|
766
|
-
Two commands allow you to open code from the debugger into an editor:
|
767
|
-
|
768
|
-
* `edit [file:line]`: edit _file_ using the editor specified by the EDITOR
|
769
|
-
environment variable. A specific _line_ can also be given.
|
770
|
-
|
771
|
-
### Quitting
|
772
|
-
|
773
|
-
To exit the debugger, use the `quit` command (abbreviated `q`), or its alias
|
774
|
-
`exit`.
|
775
|
-
|
776
|
-
A simple quit tries to terminate all threads in effect. Therefore your server
|
777
|
-
will be stopped and you will have to start it again.
|
778
|
-
|
779
|
-
### Settings
|
780
|
-
|
781
|
-
`byebug` has a few available options to tweak its behaviour:
|
782
|
-
|
783
|
-
* `set autoreload`: Reload source code when changed (default: true).
|
784
|
-
* `set autolist`: Execute `list` command on every breakpoint (default: true).
|
785
|
-
* `set listsize _n_`: Set number of source lines to list by default to _n_
|
786
|
-
(default: 10)
|
787
|
-
* `set forcestep`: Make sure the `next` and `step` commands always move to a new
|
788
|
-
line.
|
789
|
-
|
790
|
-
You can see the full list by using `help set`. Use `help set _subcommand_` to
|
791
|
-
learn about a particular `set` command.
|
792
|
-
|
793
|
-
TIP: You can save these settings in an `.byebugrc` file in your home directory.
|
794
|
-
The debugger reads these global settings when it starts. For example:
|
795
|
-
|
796
|
-
```bash
|
797
|
-
set forcestep
|
798
|
-
set listsize 25
|
799
|
-
```
|
800
|
-
|
801
|
-
Debugging Memory Leaks
|
802
|
-
----------------------
|
803
|
-
|
804
|
-
A Ruby application (on Rails or not), can leak memory - either in the Ruby code
|
805
|
-
or at the C code level.
|
806
|
-
|
807
|
-
In this section, you will learn how to find and fix such leaks by using tool
|
808
|
-
such as Valgrind.
|
809
|
-
|
810
|
-
### Valgrind
|
811
|
-
|
812
|
-
[Valgrind](http://valgrind.org/) is a Linux-only application for detecting
|
813
|
-
C-based memory leaks and race conditions.
|
814
|
-
|
815
|
-
There are Valgrind tools that can automatically detect many memory management
|
816
|
-
and threading bugs, and profile your programs in detail. For example, if a C
|
817
|
-
extension in the interpreter calls `malloc()` but doesn't properly call
|
818
|
-
`free()`, this memory won't be available until the app terminates.
|
819
|
-
|
820
|
-
For further information on how to install Valgrind and use with Ruby, refer to
|
821
|
-
[Valgrind and Ruby](http://blog.evanweaver.com/articles/2008/02/05/valgrind-and-ruby/)
|
822
|
-
by Evan Weaver.
|
823
|
-
|
824
|
-
Plugins for Debugging
|
825
|
-
---------------------
|
826
|
-
|
827
|
-
There are some Rails plugins to help you to find errors and debug your
|
828
|
-
application. Here is a list of useful plugins for debugging:
|
829
|
-
|
830
|
-
* [Footnotes](https://github.com/josevalim/rails-footnotes) Every Rails page has
|
831
|
-
footnotes that give request information and link back to your source via
|
832
|
-
TextMate.
|
833
|
-
* [Query Trace](https://github.com/ntalbott/query_trace/tree/master) Adds query
|
834
|
-
origin tracing to your logs.
|
835
|
-
* [Query Reviewer](https://github.com/nesquena/query_reviewer) This rails plugin
|
836
|
-
not only runs "EXPLAIN" before each of your select queries in development, but
|
837
|
-
provides a small DIV in the rendered output of each page with the summary of
|
838
|
-
warnings for each query that it analyzed.
|
839
|
-
* [Exception Notifier](https://github.com/smartinez87/exception_notification/tree/master)
|
840
|
-
Provides a mailer object and a default set of templates for sending email
|
841
|
-
notifications when errors occur in a Rails application.
|
842
|
-
* [Better Errors](https://github.com/charliesome/better_errors) Replaces the
|
843
|
-
standard Rails error page with a new one containing more contextual information,
|
844
|
-
like source code and variable inspection.
|
845
|
-
* [RailsPanel](https://github.com/dejan/rails_panel) Chrome extension for Rails
|
846
|
-
development that will end your tailing of development.log. Have all information
|
847
|
-
about your Rails app requests in the browser - in the Developer Tools panel.
|
848
|
-
Provides insight to db/rendering/total times, parameter list, rendered views and
|
849
|
-
more.
|
850
|
-
|
851
|
-
References
|
852
|
-
----------
|
853
|
-
|
854
|
-
* [ruby-debug Homepage](http://bashdb.sourceforge.net/ruby-debug/home-page.html)
|
855
|
-
* [debugger Homepage](https://github.com/cldwalker/debugger)
|
856
|
-
* [byebug Homepage](https://github.com/deivid-rodriguez/byebug)
|
857
|
-
* [Article: Debugging a Rails application with ruby-debug](http://www.sitepoint.com/debug-rails-app-ruby-debug/)
|
858
|
-
* [Ryan Bates' debugging ruby (revised) screencast](http://railscasts.com/episodes/54-debugging-ruby-revised)
|
859
|
-
* [Ryan Bates' stack trace screencast](http://railscasts.com/episodes/24-the-stack-trace)
|
860
|
-
* [Ryan Bates' logger screencast](http://railscasts.com/episodes/56-the-logger)
|
861
|
-
* [Debugging with ruby-debug](http://bashdb.sourceforge.net/ruby-debug.html)
|