Difference between revisions of "Log levels"

From DarkWiki
Jump to: navigation, search
(Standard logging)
(Standard logging)
Line 20: Line 20:
  
 
==Standard logging==
 
==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">
 
<syntaxhighlight lang="java">
Line 26: Line 28:
 
     public @ResponseBody ErrorResponse handleInternalServerError(HttpServletRequest request, Exception ex) {
 
     public @ResponseBody ErrorResponse handleInternalServerError(HttpServletRequest request, Exception ex) {
 
         LOG.error(
 
         LOG.error(
             "handleInternalServerError Unhandled error - " + ex.getClass().getName(),
+
             "handleInternalServerError Unhandled error - {}",
 +
            ex.getClass().getName(),
 
             ex
 
             ex
 
         );
 
         );
 
         return new ErrorResponse(
 
         return new ErrorResponse(
          ex.getClass().getName(),
+
            ex.getClass().getName(),
          ex.getMessage()
+
            ex.getMessage()
 
         );
 
         );
 
     }
 
     }
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 09:43, 28 May 2019

Introduction

Log levels are used to indicate the importance of a particular message and are important in fault detection and diagnosis.

Levels

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 is 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.
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.

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(HttpServletRequest request, Exception ex) {
        LOG.error(
            "handleInternalServerError Unhandled error - {}",
            ex.getClass().getName(),
            ex
        );
        return new ErrorResponse(
            ex.getClass().getName(),
            ex.getMessage()
        );
    }