Skip to content
This repository
Browse code

Add logging performance [ci skip]

  • Loading branch information...
commit b48bb74ef8a8bc9539d5b9c7d5fe202f37e2e99c 1 parent 34b8953
Gaurish Sharma gaurish authored

Showing 1 changed file with 31 additions and 0 deletions. Show diff stats Hide diff stats

  1. +31 0 guides/source/debugging_rails_applications.md
31 guides/source/debugging_rails_applications.md
Source Rendered
@@ -209,6 +209,37 @@ logger.tagged("BCX", "Jason") { logger.info "Stuff" } # Logs "
209 209 logger.tagged("BCX") { logger.tagged("Jason") { logger.info "Stuff" } } # Logs "[BCX] [Jason] Stuff"
210 210 ```
211 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 attibutes 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 +
212 243 Debugging with the `debugger` gem
213 244 ---------------------------------
214 245

0 comments on commit b48bb74

Please sign in to comment.
Something went wrong with that request. Please try again.