Difference between revisions of "Log levels"
(→Levels) |
(→Standard logging) |
||
| (21 intermediate revisions by the same user not shown) | |||
| Line 2: | Line 2: | ||
Log levels are used to indicate the importance of a particular message and are important in fault detection and diagnosis. | Log levels are used to indicate the importance of a particular message and are important in fault detection and diagnosis. | ||
| + | |||
| + | ==Mantra: All errors need fixing== | ||
| + | |||
| + | As errors need fixing, anything that is logged as ERROR needs a developer's attention and must be fixed. Their very existence in a log file indicates bugs; an unexpected situation is the result of it not being thought through (i.e. it's symptomatic of a bug). | ||
==Levels== | ==Levels== | ||
| + | |||
| + | ===Standard=== | ||
| + | |||
| + | These are standard definitions that demand certain actions. | ||
| − | {| | + | {| class="wikitable" |
| − | !Level!Meaning | + | !Level!!Meaning |
|- | |- | ||
| − | |ERROR| | + | ||ERROR||A serious error has occurred, and someone must take action. Normal operation is where there are no errors occurring. |
| + | |- | ||
| + | ||WARN||A problem was encountered, but has been dealt with in some manner. It may be worked around, but it could be indicative that there may be something about to go wrong. | ||
| + | |- | ||
| + | ||INFO||A useful piece of information, typically linked to a specific event. These are the log messages that appear in normal logging. | ||
| + | |- | ||
| + | ||DEBUG||A piece of diagnostic information, typically used for fault analysis. These only normally appear in test environments (including development ones). However, it may be desirable to switch nodes into debug mode under certain circumstances. | ||
| + | |} | ||
| + | |||
| + | ===Deprecated=== | ||
| + | |||
| + | These are either poorly defined or not well supported, and must therefore be avoided. | ||
| + | |||
| + | {| class="wikitable" | ||
| + | !Level!!Meaning | ||
| + | |- | ||
| + | ||TRACE||This is rarely used. It's used to indicate to the reader the path by which code flows, including all relevant arguments and parameters. Normally, DEBUG is enough. | ||
| + | |- | ||
| + | ||FATAL||This is rarely used, and is no longer supported in some loggers (e.g. [https://en.wikipedia.org/wiki/SLF4J SLF4J]). It's used to indicate to the system is about to crash. | ||
|} | |} | ||
| + | |||
| + | ==Standard logging== | ||
| + | |||
| + | If the last object is an Exception, it will be logged with its stack trace (unless you use the string insertion markers, "{}"). | ||
| + | |||
| + | <syntaxhighlight lang="java"> | ||
| + | @ExceptionHandler({RuntimeException.class}) | ||
| + | @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) | ||
| + | public @ResponseBody ErrorResponse handleInternalServerError(Exception ex) { | ||
| + | LOG.error( | ||
| + | "handleInternalServerError Unhandled error - {}", | ||
| + | ex.getClass().getName(), | ||
| + | ex | ||
| + | ); | ||
| + | return new ErrorResponse( | ||
| + | ex.getClass().getName(), | ||
| + | ex.getMessage() | ||
| + | ); | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | While sometimes useful, it is generally bad practice to show a stack trace to a third party (such as a user). In some circumstances, it could be regarded as a security failure (e.g. [https://www.owasp.org/index.php/Testing_for_Stack_Traces_(OTG-ERR-002) OTG-ERR-002]). | ||
Latest revision as of 09:20, 11 June 2019
Contents
Introduction
Log levels are used to indicate the importance of a particular message and are important in fault detection and diagnosis.
Mantra: All errors need fixing
As errors need fixing, anything that is logged as ERROR needs a developer's attention and must be fixed. Their very existence in a log file indicates bugs; an unexpected situation is the result of it not being thought through (i.e. it's symptomatic of a bug).
Levels
Standard
These are standard definitions that demand certain actions.
| Level | Meaning |
|---|---|
| ERROR | A serious error has occurred, and someone must take action. Normal operation is where there are no errors occurring. |
| WARN | A problem was encountered, but has been dealt with in some manner. It may be worked around, but it could be indicative that there may be something about to go wrong. |
| INFO | A useful piece of information, typically linked to a specific event. These are the log messages that appear in normal logging. |
| DEBUG | A piece of diagnostic information, typically used for fault analysis. These only normally appear in test environments (including development ones). However, it may be desirable to switch nodes into debug mode under certain circumstances. |
Deprecated
These are either poorly defined or not well supported, and must therefore be avoided.
| Level | Meaning |
|---|---|
| TRACE | This is rarely used. It's used to indicate to the reader the path by which code flows, including all relevant arguments and parameters. Normally, DEBUG is enough. |
| FATAL | This is rarely used, and is no longer supported in some loggers (e.g. SLF4J). It's used to indicate to the system is about to crash. |
Standard logging
If the last object is an Exception, it will be logged with its stack trace (unless you use the string insertion markers, "{}").
@ExceptionHandler({RuntimeException.class})
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public @ResponseBody ErrorResponse handleInternalServerError(Exception ex) {
LOG.error(
"handleInternalServerError Unhandled error - {}",
ex.getClass().getName(),
ex
);
return new ErrorResponse(
ex.getClass().getName(),
ex.getMessage()
);
}
While sometimes useful, it is generally bad practice to show a stack trace to a third party (such as a user). In some circumstances, it could be regarded as a security failure (e.g. OTG-ERR-002).