Registering with Rails, is there any performance?

Rails comes bundled with the Ruby logger class in the standard library. Available log levels are :debug :info :warn :error and :fatal .

I was interested to know if I can add extensive logging in my Rails application with the log level set to :debug for development and testing, whether there will be a performance impact on startup in production with disabling or installing at a higher level, such as config.log_level = :fatal ?

+7
source share
1 answer

The short answer is that logging will always have an impact on performance, especially when registering on disk. However, there are several subtleties.

Firstly, using the :debug level will have a greater performance limit than :fatal , since a much larger number of lines are evaluated and written to the output of the log (for example, a disk).

Another potential error is that if your code has many of these calls:

 logger.debug = "my string to debug with a #{variable}" 

The effect will affect performance, even if a valid output level does not include debugging. The reason is because Ruby needs to evaluate these strings, which includes instantiating a somewhat heavy String object and interpolating the variables, and this takes time.

Therefore, it is recommended to transfer the blocks to the registration methods, since they are evaluated only if the output level is the same or included in an acceptable level (i.e., lazy loading). The same rewritten code:

 logger.debug { "my string to debug with a #{variable}" } 

The contents of the block and, therefore, line interpolation are evaluated only if debug is enabled. This performance savings is only very noticeable with a lot of registration, but it is good practice to use.

For more information, see Logger Docs .

+17
source

All Articles