aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPiotr Sarnacki <drogus@gmail.com>2013-07-21 16:19:10 -0700
committerPiotr Sarnacki <drogus@gmail.com>2013-07-21 16:19:10 -0700
commit2c1ddd82e68d24e793a7e80076459455aa8fb5d6 (patch)
tree722862c0b1a4be6fb386d3dc76d6241d6e7061ae
parenta836db0244265a8157bce7d979210e5ad4eb47e8 (diff)
parentb48bb74ef8a8bc9539d5b9c7d5fe202f37e2e99c (diff)
downloadrails-2c1ddd82e68d24e793a7e80076459455aa8fb5d6.tar.gz
rails-2c1ddd82e68d24e793a7e80076459455aa8fb5d6.tar.bz2
rails-2c1ddd82e68d24e793a7e80076459455aa8fb5d6.zip
Merge pull request #11534 from gaurish/log
Add logging performance [ci skip]
-rw-r--r--guides/source/debugging_rails_applications.md31
1 files changed, 31 insertions, 0 deletions
diff --git a/guides/source/debugging_rails_applications.md b/guides/source/debugging_rails_applications.md
index 77a2dd4b18..50ee934b87 100644
--- a/guides/source/debugging_rails_applications.md
+++ b/guides/source/debugging_rails_applications.md
@@ -209,6 +209,37 @@ logger.tagged("BCX", "Jason") { logger.info "Stuff" } # Logs "
logger.tagged("BCX") { logger.tagged("Jason") { logger.info "Stuff" } } # Logs "[BCX] [Jason] Stuff"
```
+### Impact of Logs on Performance
+Logging will always have a small impact on performance of your rails app,
+ particularly when logging to disk.However, there are a few subtleties:
+
+Using the `:debug` level will have a greater performance penalty than `:fatal`,
+ as a far greater number of strings are being evaluated and written to the
+ log output (e.g. disk).
+
+Another potential pitfall is that if you have many calls to `Logger` like this
+ in your code:
+
+```ruby
+logger.debug "Person attributes hash: #{@person.attributes.inspect}"
+```
+
+In the above example, There will be a performance impact even if the allowed
+output level doesn't include debug. The reason is that Ruby has to evaluate
+these strings, which includes instantiating the somewhat heavy `String` object
+and interpolating the variables, and which takes time.
+Therefore, it's recommended to pass blocks to the logger methods, as these are
+only evaluated if the output level is the same or included in the allowed level
+(i.e. lazy loading). The same code rewritten would be:
+
+```ruby
+logger.debug {"Person attibutes hash: #{@person.attributes.inspect}"}
+```
+
+The contents of the block, and therefore the string interpolation, is only
+evaluated if debug is enabled. This performance savings is only really
+noticeable with large amounts of logging, but it's a good practice to employ.
+
Debugging with the `debugger` gem
---------------------------------