andreas1234567 has asked for the
wisdom of the Perl Monks concerning the following question:

I'm looking for a Perl::Critic policy related to Log::Log4perl. The policy should require every logging statement to be surrounded by a conditional testing for the given log level, as shown here. Running Devel::NYTProf has shown that this code, as noted by the Log::Log4perl module's author, can have a significant negative impact on performance.

Please read that link to Log4perl again. The Log4perl author is *not* suggesting that every logging call should be wrapped, but rather that *expensive* calls should be. Since only the application developer can be expected to be able to tell which calls would be expensive, IMHO using such a Perl::Critic policy (if it exists) would be both code bloat and slow down the application, exactly the opposite to the effect the OP seems to be trying to achieve.

The following Benchmarked code snipped shows a 10-15% performance increase by simply wrapping the log calls. Note that it is just a scalar being logged (suppressed, actually), not even an array, an especially not a super-long array. I don't find it to be particularly bloated either:

Why? Because even the call to is_debug takes twice as long as the rest, the "meat" of your loop. Obviously you should have a perl_critic rule to warn from use of log4perl at all. Or avoid artificial benchmarks ;-)

How would Perl::Critic find out that @super_long_array is in fact a super long array? Note that the negative impact is only when the error message is really long and takes ages to expand. In the normal case warnings or error messages would be relatively short and the impact nonexistant.

I first wanted to reply that this would be nonsense because nobody puts big arrays into error logs (and I still stand by that). A perl_critic rule that produces 99% false positives is not a good perl_critic rule. But then it occured to me that debug-logs might have such calls more frequently.

So you got a point, but only in so far as logging level 'debug' is concerned. You might flag all calls to debug that don't have such a if-clause and you actually would have a somewhat sensible rule IMO.

No because the string will be interpolated on the way in, so you won't avoid the hit. The only way to avoid the hit is to ensure the string never gets interpolated in the first place (it's not the log call that's slow, it's the string interpolation of a huge array).