I am developing an Apache module. During development I found it convenient to use logging functions at various points in my code.
For example after opening a file, I would log an error if the operation was not successful so that I may know exactly where the problem occurred in my code.
Now, I am about to deliver my module to my boss (I am on an internship). I wanted to what are the best practices regarding logging. Is it good for maintenance purposes or is it bad because it may hamper the response time of the server.
It really depends on how you wrote those logging instructions. If you wrote:
You might affect performance badly if the logger is not set on a DEBUG level (
computeSomeCostlyDebugOutputwill always take time to execute and its result will then be ignored by the logger if not matching the DEBUG level).If you write it like this instead:
then the costly operations and logging will occur only if the correct logger level is set (i.e. the logger won’t ignore it). It basically acts like another switch for the logger, the first switch being the configured logger level.
As Andrzej Doyle very well pointed out, the logger will check its level internally, but this happens in the
debugmethod, after time was already wasted in thecomputeSomeCostlyDebugOutput. If you waste time incomputeSomeCostlyDebugOutput, you better do it when you know that its result won’t be in vain.A lot of software ships with logging instructions which you can activate if you need more details into the inner workings of the thing, execution path and stuff. Just make sure they can be deactivated and only take computing time if the appropriate level is set.