ruby - Logging in Rails, is there any performance hit? -


rails comes bundled ruby's logger class in standard library. available log levels are: :debug, :info, :warn, :error, , :fatal.

i wondering if add extensive logging in rails application log level set :debug development , testing, there performance impact when running in production logging turned-off or set @ higher level, such config.log_level = :fatal?

the short answer logging have performance impact, particularly when logging disk. however, there few subtleties.

firstly, using :debug level have greater performance penalty :fatal, far greater number of strings being evaluated , written log output (e.g. disk).

another potential pitfall if have many calls in code:

logger.debug = "my string debug #{variable}" 

there performance impact if allowed output level doesn't include debug. reason ruby has evaluate these strings, includes instantiating heavy string object , interpolating variables, , takes time.

therefore, it's recommended pass blocks logger methods, these evaluated if output level same or included in allowed level (i.e. lazy loading). same code rewritten be:

logger.debug { "my string debug #{variable}" } 

the contents of block, , therefore string interpolation, evaluated if debug enabled. performance savings noticeable large amounts of logging, it's practice employ.

you can read more in logger docs.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -