Logging is the process of recording application actions and state to a secondary interface.
Logging System 3
|Level||When it’s used|
|DEBUG||Detailed information, typically of interest only when diagnosing problems.|
|INFO||Confirmation that things are working as expected.|
|WARNING||An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.|
|Due to a more serious problem, the software has not been able to perform some function.|
|CRITICAL||A serious error, indicating that the program itself may be unable to continue running.|
- Logging should always be considered when handling an exception but should never take the place of a real handler.
- Keep all logging code in your production code. Have an ability to enable more/less detailed logging in production, preferably per subsystem and without restarting your program.
- Make logs easy to parse by grep and by eye. Stick to several common fields at the beginning of each line. Identify time, severity, and subsystem in every line. Clearly formulate the message. Make every log message easy to map to its source code line.
- If an error happens, try to collect and log as much information as possible. It may take long but it's OK because normal processing has failed anyway. Not having to wait when the same condition happens in production with a debugger attached is priceless.