logging

Can I use logs instead comments in my code?


Is anybody using logs instead of comments? Is it right?

For example:

logger.debug('Fingerprint is not specified. Returning only message that password is correct in response.');
logger.info('Password is correct. But fingerprintId was not passed. User ID:', foundUser.id);
logger.debug('Returning success message in response.');

Logging in this case does 2 things: logging and commenting.

Is it common practice?


Solution

  • I believe that comments and logging just serve for different purposes.

    Logging is done to see what your program is doing right now or what it was doing at a specific point. For example, you open a log file and check what was going on five minutes ago and what's happening now. That's useful when you don't really bother at which line or in which method exactly is something happening, you're more interested in facts, like 'User admin failed to login: incorrect password'. Obviously, that helps a lot when you don't have source code of the program.

    Comments help you to understand what this code does, what this particular function is doing, why this variable is here and how is all this supposed to work. But if you've got just a binary, comments are totally useless.

    To my mind, you simply can't mess it all together since comments and logging are different.