-
Notifications
You must be signed in to change notification settings - Fork 2.2k
CustomCheckStyleRules: Good Logging Practice #3359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mssfang
merged 42 commits into
Azure:master
from
mssfang:CheckStyleRule-GoodLoggingPractice
Aug 27, 2019
Merged
Changes from 3 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
162ffdc
feature(GoodLoggingPractice): init draft but not error produced
7dbcf10
resolve conflict
609940a
fix(space): no extra empty space
5f6f36e
feat(fix): refactor based on Connie' code change request
408a372
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
c342be4
feat(import): remove unused import lib
d6704ee
Merge branch 'master' into CheckStyleRule-GoodLoggingPractice
mssfang fcdc012
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
cf277b4
feat(update): corrected and tested
b5c0225
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
e2531ff
Merge branch 'CheckStyleRule-GoodLoggingPractice' of https://github.c…
ebdcb13
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
77225db
while loop works
32793cd
working solution for guard blocked isSomethingEnabled
36390d8
resolve stash pop conflict
66c99db
fix: refactor all based on the new unstanding
95a00bd
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
8479eea
resolve conflict since May
6b1c866
refactor and fix bugs
694370c
resolve conflict
c2fa02f
based on new feedback and fixes
1187938
fixes errors on Identity service
55bbbf9
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
caa685e
resolve conflict
239f621
fixes based on Srekanta's feedback
9781106
resolve conflict
2fb3108
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
06cf258
fixes based on feedback
95e4545
resolve conflict
87d45ed
resolve conflict
adcb2a9
add java.util.logging
871e182
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
6c449c3
updates docs and minor changes
f5c4090
no throw change
b43780f
refactor
10af7b6
resolve conflict
bd73a70
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
8adfc66
resolve conflict
e7a3dc5
Update eng/code-quality-reports/src/main/java/com/azure/tools/checkst…
mssfang 98a3227
re-wording
044c19c
Merge branch 'CheckStyleRule-GoodLoggingPractice' of https://github.c…
9937e0d
Update eng/code-quality-reports/src/main/java/com/azure/tools/checkst…
mssfang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
225 changes: 225 additions & 0 deletions
225
...ode-quality-reports/src/main/java/com/azure/tools/checkstyle/checks/GoodLoggingCheck.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.tools.checkstyle.checks; | ||
|
|
||
| import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | ||
| import com.puppycrawl.tools.checkstyle.api.DetailAST; | ||
| import com.puppycrawl.tools.checkstyle.api.TokenTypes; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Check Style Rule: Good Logging Practice | ||
| * I. Non-static Logger instance | ||
| * II. Logger variable name should be 'logger' | ||
| * III. Guard with conditional block check whenever logger's logging method called. | ||
| */ | ||
| public class GoodLoggingCheck extends AbstractCheck { | ||
| private static final String LOGGER_CLASS = "Logger"; | ||
| private static final String LOGGER_NAME = "logger"; | ||
|
|
||
| private static final String LOGGER_NAME_ERR = "Incorrect name for Logger: use 'logger' instead of '%s' as Logger name"; | ||
| private static final String LOGGER_LEVEL_ERR = "Please guard %s method call with %s method"; | ||
| private static final String STATIC_LOGGER_ERR = "Logger should not be static: remove static modifier"; | ||
| private static final int[] TOKENS = new int[] { | ||
| TokenTypes.METHOD_CALL, | ||
| TokenTypes.VARIABLE_DEF | ||
| }; | ||
|
|
||
| private static final Map<String, String> loggerMethodMap = new HashMap<>(); | ||
|
|
||
| // Logger level static final string variables | ||
| private static final String FATAL = "fatal"; | ||
| private static final String ERROR = "error"; | ||
| private static final String WARN = "warn"; | ||
| private static final String INFO = "info"; | ||
| private static final String DEBUG = "debug"; | ||
| private static final String TRACE = "trace"; | ||
|
|
||
| private static final String IS_FATAL_ENABLED = "isFatalEnabled"; | ||
| private static final String IS_ERROR_ENABLED = "isErrorEnabled"; | ||
| private static final String IS_WARN_ENABLED = "isWarnEnabled"; | ||
| private static final String IS_INFO_ENABLED = "isInfoEnabled"; | ||
| private static final String IS_DEBUG_ENABLED = "isDebugEnabled"; | ||
| private static final String IS_TRACE_ENABLED = "isTraceEnabled"; | ||
|
|
||
| private static final Set<String> loggerSet = new HashSet<>(); | ||
|
|
||
| @Override | ||
| public void init() { | ||
| initLogLevelMap(); | ||
|
mssfang marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| @Override | ||
| public int[] getDefaultTokens() { | ||
| return getRequiredTokens(); | ||
| } | ||
|
|
||
| @Override | ||
| public int[] getRequiredTokens() { | ||
| return TOKENS; | ||
| } | ||
|
|
||
| @Override | ||
| public int[] getAcceptableTokens() { | ||
| return getRequiredTokens(); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitToken(DetailAST ast) { | ||
|
|
||
| // skip check if no Logger class imported | ||
| if (!hasLoggerImport(ast)) { | ||
| return; | ||
| } | ||
|
|
||
| switch (ast.getType()) { | ||
| case TokenTypes.VARIABLE_DEF: | ||
| isNameLogger(ast); | ||
| isNonStaticLoggerInstance(ast); | ||
| break; | ||
| case TokenTypes.METHOD_CALL: | ||
| isGuardConditionChecked(ast); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check if the file has Logger class imported | ||
| * @param rootAST The root of AST | ||
| * @return true if the class has Logger class imported. | ||
| */ | ||
| private boolean hasLoggerImport(DetailAST rootAST) { | ||
| DetailAST importToken = rootAST.findFirstToken(TokenTypes.IMPORT); | ||
|
mssfang marked this conversation as resolved.
Outdated
|
||
| while (importToken != null && importToken.getType() == TokenTypes.IMPORT ) { | ||
| String importClassName = (importToken.findFirstToken(TokenTypes.DOT)).getLastChild().getText(); | ||
| if (importClassName.equals(LOGGER_CLASS)) { | ||
| return true; | ||
| } | ||
| importToken = importToken.getNextSibling(); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Check if the Logger instance named as logger, log as error if not | ||
| * @param varDefAST the VARIABLE_DEF node | ||
| */ | ||
| private void isNameLogger(DetailAST varDefAST) { | ||
| String type = varDefAST.findFirstToken(TokenTypes.TYPE).getFirstChild().getText(); | ||
| if (type.equals(LOGGER_CLASS)) { | ||
|
mssfang marked this conversation as resolved.
Outdated
|
||
| String varName = varDefAST.findFirstToken(TokenTypes.IDENT).getText(); | ||
| loggerSet.add(varName); | ||
|
mssfang marked this conversation as resolved.
Outdated
|
||
| if (!varName.equals(LOGGER_NAME)) { | ||
| log(varDefAST.getLineNo(), String.format(LOGGER_NAME_ERR, varName)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check if the Logger is static instance, log as error if static instance logger, | ||
| * @param varDefAST the VARIABLE_DEF node | ||
| */ | ||
| private void isNonStaticLoggerInstance(DetailAST varDefAST) { | ||
|
|
||
| String type = varDefAST.findFirstToken(TokenTypes.TYPE).getFirstChild().getText(); | ||
| if (type.equals(LOGGER_CLASS)) { | ||
| if(varDefAST.findFirstToken(TokenTypes.MODIFIERS).branchContains(TokenTypes.LITERAL_STATIC)) { | ||
| log(varDefAST.getLineNo(), STATIC_LOGGER_ERR); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Always guard with conditional block whenever a log function called. | ||
| * @param methodCallAST The METHOD_CALL node | ||
| */ | ||
| private void isGuardConditionChecked(DetailAST methodCallAST) { | ||
| DetailAST loggerName = methodCallAST.findFirstToken(TokenTypes.DOT).getFirstChild(); | ||
| // only check for logger instance, skip checking otherwise | ||
| if (!loggerSet.contains(loggerName.getText())) { | ||
| return; | ||
| } | ||
|
|
||
| DetailAST methodName = methodCallAST.findFirstToken(TokenTypes.DOT).getLastChild(); | ||
| String methodNameStr = methodName.getText(); | ||
| if (isLogLevelMatched(methodCallAST, methodNameStr)) { | ||
|
mssfang marked this conversation as resolved.
Outdated
|
||
| switch (methodNameStr) { | ||
| case FATAL: // log4j only | ||
|
mssfang marked this conversation as resolved.
Outdated
|
||
| log(methodCallAST.getLineNo(), String.format(LOGGER_LEVEL_ERR, methodNameStr, IS_FATAL_ENABLED)); | ||
| break; | ||
| case ERROR: | ||
| log(methodCallAST.getLineNo(), String.format(LOGGER_LEVEL_ERR, methodNameStr, IS_ERROR_ENABLED)); | ||
| break; | ||
| case WARN: | ||
| log(methodCallAST.getLineNo(), String.format(LOGGER_LEVEL_ERR, methodNameStr, IS_WARN_ENABLED)); | ||
| break; | ||
| case INFO: | ||
| log(methodCallAST.getLineNo(), String.format(LOGGER_LEVEL_ERR, methodNameStr, IS_INFO_ENABLED)); | ||
| break; | ||
| case DEBUG: | ||
| log(methodCallAST.getLineNo(), String.format(LOGGER_LEVEL_ERR, methodNameStr, IS_DEBUG_ENABLED)); | ||
| break; | ||
| case TRACE: | ||
| log(methodCallAST.getLineNo(), String.format(LOGGER_LEVEL_ERR, methodNameStr, IS_TRACE_ENABLED)); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param methodCallRoot METHOD_CALL node which contains logging method call, such as logger.debug() | ||
| * @param loggingLevelName logging level method name, such as debug, error, info, etc. | ||
| * @return return true if the logging level match with guard condition block check, otherwise, return false. | ||
| */ | ||
| private boolean isLogLevelMatched(DetailAST methodCallRoot, String loggingLevelName) { | ||
| if (methodCallRoot == null || methodCallRoot.getParent() == null) { | ||
|
mssfang marked this conversation as resolved.
Outdated
|
||
| return false; | ||
| } | ||
|
|
||
| DetailAST slistAST = methodCallRoot.getParent().getParent(); | ||
| if(slistAST == null || slistAST.getType()!= TokenTypes.SLIST ) { | ||
| return false; | ||
| } | ||
|
|
||
| DetailAST ifAST = slistAST.getParent(); | ||
| if (ifAST == null || ifAST.getType() != TokenTypes.LITERAL_IF) { | ||
| return false; | ||
| } | ||
|
|
||
| DetailAST exprAST = ifAST.findFirstToken(TokenTypes.EXPR); | ||
| if (exprAST == null) { | ||
| return false; | ||
| } | ||
|
|
||
| DetailAST methodCallAST = exprAST.findFirstToken(TokenTypes.METHOD_CALL); | ||
| if(methodCallAST == null) { | ||
| return false; | ||
| } | ||
|
|
||
| DetailAST dotAST = methodCallAST.findFirstToken(TokenTypes.DOT); | ||
|
|
||
| if (!(dotAST.getLastChild().getType() == TokenTypes.IDENT) | ||
| || !dotAST.getLastChild().getText().equals(loggerMethodMap.get(loggingLevelName))) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * initialize the log level mapping | ||
| */ | ||
| private void initLogLevelMap() { | ||
| loggerMethodMap.put(TRACE, IS_TRACE_ENABLED); | ||
| loggerMethodMap.put(DEBUG, IS_DEBUG_ENABLED); | ||
| loggerMethodMap.put(INFO, IS_INFO_ENABLED); | ||
| loggerMethodMap.put(WARN, IS_WARN_ENABLED); | ||
| loggerMethodMap.put(ERROR, IS_ERROR_ENABLED); | ||
| loggerMethodMap.put(FATAL, IS_FATAL_ENABLED); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.