Rails comes bundled with Ruby's logger
class in the standard library. The available log levels are: :debug
, :info
, :warn
, :error
, and :fatal
.
I was wondering if I add extensive logging in my Rails application with log level set to :debug
for development and testing, will there be a performance impact when running in production with logging turned-off or set at higher level, such as config.log_level = :fatal
?
Logs Can Have a Strong Impact on Stability, Performance, and Garbage Collection.
logger level is not thread safe. The main issue appears to be that Rails. logger is a singleton.
Rails uses six different log levels: debug, info, warn, error, fatal, and unknown. Each level defines how much information your application will log: Debug: diagnostic information for developers and system administrators, including database calls or inspecting object attributes.
In a Rails app, logs are stored under the /log folder. In development mode, the development. log file is used & you see log output on the terminal you're running rails server on.
The short answer is that logging will always have a performance impact, particularly when logging to disk. However, there are a few subtleties.
Firstly, 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 like this in your code:
logger.debug = "my string to debug with a #{variable}"
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 that 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:
logger.debug { "my string to debug with a #{variable}" }
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.
You can read more about this in the Logger docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With