When I add logging (e.g. log4j2 in Java) to existing code, is it ok (good practice), to alter the code?
Consider the following example:
//if process returned 0 return true
return (returnCode == process.waitFor());
This construction is rather neat, but when I want to add logging to it, and I want to use two different levels of logging (DEBUG
vs ERROR
) depending on the process return code, it can change to something like this:
int returnCode = process.waitFor();
if(returnCode == 0)
{
logger.debug("Command returned code 0");
}
else
{
logger.error("Command returned code: ");
}
return (returnCode == 0);
Another example would be:
...
throw new UnsupportedOperationException("Unknown message: " + message.toString());
changed to:
String errorMsg = "Unknown message: " + message.toString();
logger.error(errorMsg);
throw new UnsupportedOperationException(errorMsg);
Is altering the code in order to add logs acceptable? Are there some guidelines on how to do it?
return
statements could only return simple values, and not the result of a function call or comparison. This made adding logging statements easier (as well as debugging).