Eric posted today about log level confusion. As with everything, I have a long-winded and obsessive opinion about this so I thought I’d share it so the blogosphere can tell me how wrong I am. (Sorry, many days with no blogging has made me feisty!)
My opinion is that there are 4 log levels that matter for general program logging (specialized logs like audit and performance have their own rules). I don’t care what you call them so feel free to replace these with the names of your choice.
- ERROR – an error has occurred in the program (usually an exception). All internal or unexpected failures should be logged at ERROR level. Programmers should care about anything logged as an error. User errors (like input validation) should generally NOT be logged at ERROR level. These are expected (if invalid) inputs and you should respond to the user appropriately (another post altogether), but this is not a program error. Exceptions may be made for things like repeated invalid password attempts that are likely security problems.
- WARNING – an anomalous condition has been detected and the program will attempt to deal with it. No action needs to be taken externally but you might be interested if things go down in flames 5 minutes later. The conditions for warnings are relatively rare but an example would be reaching a warning threshold on a connection pool or a loss of connectivity that can be repaired by reconnecting to the same or different source.
- INFO – an interesting piece of information that helps to give context to a log, often when things are starting or stopping. INFO messages should never be printed on a “per transaction” (whatever that means to you) path through the code. That is, INFO messages should be 1-time or very occasional messages.
- DEBUG – whatever your OCD programmer mind desires.
The first three levels (ERROR, WARNING, and INFO) should always be on. If your logs grow rapidly with these levels on, something is wrong. You should be able to run in production with these levels for weeks without a problem (but you should really use a rolling log writer just in case). DEBUG should be used only during development for debugging.
On the subject of hierarchical loggers, I generally find them to be more annoying than useful. If you do use them, I really prefer to use them with the DEBUG level for turning on a targeted set of debug logging, not for changing what comes out of ERROR, WARNING, and INFO.