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 .
Jon cairns
source share