Assuming you know about “guarded logging”, a Java logging best
practise. If not, get an overview in this IBM article or in this blog.
Basically it is the practice of checking if a particular log message
will indeed be outputted to the log (based on the severity level)
before calling the log statement. This is a performance improvement.
Here’s a Java example:
So I heard from one of my colleagues that Ruby’s logging performance
can also be improved the same way, by putting the message in a block
(instead of just passing it as an string arg to the method):
See various ways to log a message at the ruby-docs (search for
“How to log a message” in that page). I looked into the source-code
and, just like in Java, the first thing that is done is to check the
severity and return (true) if the severity is higher than the
message’s. So like in Java, it should not have any effect (except
creating the strings etc.).
Just to test it out, I wrote a benchmark test:
Here’s what I noticed:
with n <= 10,000 (number of logging calls), there was really no
difference between the three ways:
with n = 100,000: It looked like the simplest way to log (with a
string-arg) still performs the best:
Final takeaway: it really does not make that big a difference and
keeping it simple (with a string-arg) should work. If you are worried
about performance, this probably is not a trick that will give you a
big bang for the buck.