Mar 19, 2011

Logging Best Pratice (Draft #0)

There are 5 levels of logging to utilize: DEBUG, INFO, WARNING, ERROR, and FATAL.

DEBUG
We should log these 4 entities:

1. Entry point of a method with parameters
2. Exit point of a method with returned values
3. the condition of a if-else and which branch it results in
4. every iteration of a loop with critical data

INFO
The general purpose of INFO level is to provide contextual information.

For example, you should log when a user logs in and also log the result of logging in. (Please also provide parameters information and results if it does not incur too much CPU cost).


WARNING
We should log some event as a warning when there is something wrong but it is not the code's fault.

For example,
1. user calls for a non-existing entity (the id is invalid).
2. Invalid status or unexpected status.
3. User is not logged and he/she is not supposed to have access to this method.

The general purpose of WARNING level is to reveal security problems.

ERROR
We should log all exceptions thrown in try-catch blocks. Please do include traces in the log.

FATAL
At FATAL level, we only log when the server is going to crash. I cannot think of any event that we have to log right now.

Besides, the server automatically logs errors for us when a fatal error occurs.