Log performance penalties may still be incurred at quieter log levels

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the logging category.

Last Updated: 2024-12-03

Say I have this line of code:

logger.debug "#{current_user.name}" 

Even if I am using the info level logger, which would not log this line, the program still evaluates that string prior to deciding whether to log. Because fetching the current_user performs a DB query, this can slow the program down enormously despite the logging being off.

What you really want to do is delay execution by using a lambda/anon function-like structure:

logger.debug { "#{current_user.name}" }

Lesson

Ensure that your logging doesn't cause unnecessary slow-down by using lambdas which can be switched off for execution during the quieter log levels.