Class EitherLogOrThrowCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.github.sevntu.checkstyle.checks.coding.EitherLogOrThrowCheck
-
- All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable
,com.puppycrawl.tools.checkstyle.api.Contextualizable
public class EitherLogOrThrowCheck extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
Either log the exception, or throw it, but never do both. Logging and throwing results in multiple log messages for a single problem in the code, and makes problems for the support engineer who is trying to dig through the logs. This is one of the most annoying error-handling antipatterns. All of these examples are equally wrong.
Examples:
catch (NoSuchMethodException e) { LOG.error("Message", e); throw e; }
orcatch (NoSuchMethodException e) { LOG.error("Message", e); throw new MyServiceException("AnotherMessage", e); }
orcatch (NoSuchMethodException e) { e.printStackTrace(); throw new MyServiceException("Message", e); }
What check can detect:
Loggers- logger is declared as class field
- logger is declared as method's local variable
- logger is declared as local variable in
catch
block - logger is passed through method's parameters
- logger logs
catch
parameter exception or its message - throw
catch
parameter exception - throw another exception which is based on
catch
parameter exception - printStackTrace was called on
catch
parameter exception
What check can not detect:
- loggers that is used like method's return value. Example:
getLogger().error("message", e)
- loggers that is used like static fields from another classes:
MyAnotherClass.LOGGER.error("message", e);
Default parameters are:
- loggerFullyQualifiedClassName - fully qualified class name of logger type. Default value is "org.slf4j.Logger".
- loggingMethodNames - comma separated names of logging methods. Default value is "error, warn, info, debug".
Note that check works with only one logger type. If you have multiple different loggers, then create another instance of this check.
- Since:
- 1.9.0
- Author:
- Baratali Izmailov
-
-
Constructor Summary
Constructors Constructor Description EitherLogOrThrowCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]
getAcceptableTokens()
int[]
getDefaultTokens()
int[]
getRequiredTokens()
void
setLoggerFullyQualifiedClassName(String loggerFullyQualifiedClassName)
Set logger full class name and logger simple class name.void
setLoggingMethodNames(String... loggingMethodNames)
Set logging method names.void
visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
-
-
-
Field Detail
-
MSG_KEY
public static final String MSG_KEY
Key for error message.- See Also:
- Constant Field Values
-
-
Method Detail
-
setLoggerFullyQualifiedClassName
public void setLoggerFullyQualifiedClassName(String loggerFullyQualifiedClassName)
Set logger full class name and logger simple class name.- Parameters:
loggerFullyQualifiedClassName
- Logger full class name. Example: org.slf4j.Logger.
-
setLoggingMethodNames
public void setLoggingMethodNames(String... loggingMethodNames)
Set logging method names.- Parameters:
loggingMethodNames
- Logger method names.
-
getDefaultTokens
public int[] getDefaultTokens()
- Specified by:
getDefaultTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getAcceptableTokens
public int[] getAcceptableTokens()
- Specified by:
getAcceptableTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getRequiredTokens
public int[] getRequiredTokens()
- Specified by:
getRequiredTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
visitToken
public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
- Overrides:
visitToken
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
-