Log level usage
In production the log level should be INFO. However, in many cases it is set to DEBUG because otherwise critical message are missed. This is unfortunate, because it usually leads to a lot of log file pollution. And it should not have been needed, had the developers followed the following rules.
Log level usage
Log level rules:
-
TRACE: Use only very rarely, because your debugger is usually better for tracing the flow in the code. However, if you are dealing with code that requires the log level to be DEBUG in production you could use this as pseudo-DEBUG level, knowing these loggings will not be done in production. Much better of course would be to refactor the code and modify the pseudo-INFO loggings (
debug
loggings that should have beeninfo
) intoinfo
. But that may require a lot of work. So until then you could use TRACE as pseudo-DEBUG when DEBUG is still used as pseudo-INFO. -
DEBUG: Use for developing and testing only.
-
INFO: The level production should be at.
-
WARN: For error situations that can be handled by the system itself, eg. an invalid request from the client, so send appropriate error response back.
-
ERROR: Major error, requires manual intervention from Ops and/or Dev.
-
FATAL: Fatal error, system must stop to prevent further damage. So this should always be followed by a system shutdown, eg.
System.exit(SOME_ERROR_CODE)
.
In case of an exception, log the stack trace unless it is an expected exception. Logging the exception can occur at all levels, albeit usually at higher levels (WARN or higher). If no stack trace is logged, then as least the exception message should be logged.
Additionally, make sure a log message is unique so that one easily can find it in the code base. And it should contain context, eg. values of the main parameters that were involved.