Skip to content
Merged
Show file tree
Hide file tree
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
Apr 12, 2019
7dbcf10
resolve conflict
Apr 12, 2019
609940a
fix(space): no extra empty space
Apr 12, 2019
5f6f36e
feat(fix): refactor based on Connie' code change request
Apr 16, 2019
408a372
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
Apr 16, 2019
c342be4
feat(import): remove unused import lib
Apr 16, 2019
d6704ee
Merge branch 'master' into CheckStyleRule-GoodLoggingPractice
mssfang Apr 16, 2019
fcdc012
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
Apr 18, 2019
cf277b4
feat(update): corrected and tested
Apr 23, 2019
b5c0225
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
Apr 23, 2019
e2531ff
Merge branch 'CheckStyleRule-GoodLoggingPractice' of https://github.c…
Apr 23, 2019
ebdcb13
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
May 8, 2019
77225db
while loop works
May 10, 2019
32793cd
working solution for guard blocked isSomethingEnabled
May 13, 2019
36390d8
resolve stash pop conflict
May 14, 2019
66c99db
fix: refactor all based on the new unstanding
May 14, 2019
95a00bd
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
May 16, 2019
8479eea
resolve conflict since May
Aug 1, 2019
6b1c866
refactor and fix bugs
Aug 2, 2019
694370c
resolve conflict
Aug 19, 2019
c2fa02f
based on new feedback and fixes
Aug 19, 2019
1187938
fixes errors on Identity service
Aug 19, 2019
55bbbf9
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
Aug 19, 2019
caa685e
resolve conflict
Aug 19, 2019
239f621
fixes based on Srekanta's feedback
Aug 19, 2019
9781106
resolve conflict
Aug 19, 2019
2fb3108
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
Aug 20, 2019
06cf258
fixes based on feedback
Aug 20, 2019
95e4545
resolve conflict
Aug 20, 2019
87d45ed
resolve conflict
Aug 20, 2019
adcb2a9
add java.util.logging
Aug 21, 2019
871e182
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
Aug 23, 2019
6c449c3
updates docs and minor changes
Aug 25, 2019
f5c4090
no throw change
Aug 25, 2019
b43780f
refactor
Aug 25, 2019
10af7b6
resolve conflict
Aug 26, 2019
bd73a70
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
Aug 26, 2019
8adfc66
resolve conflict
Aug 27, 2019
e7a3dc5
Update eng/code-quality-reports/src/main/java/com/azure/tools/checkst…
mssfang Aug 27, 2019
98a3227
re-wording
Aug 27, 2019
044c19c
Merge branch 'CheckStyleRule-GoodLoggingPractice' of https://github.c…
Aug 27, 2019
9937e0d
Update eng/code-quality-reports/src/main/java/com/azure/tools/checkst…
mssfang Aug 27, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
Comment thread
mssfang marked this conversation as resolved.
Outdated
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();
Comment thread
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);
Comment thread
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)) {
Comment thread
mssfang marked this conversation as resolved.
Outdated
String varName = varDefAST.findFirstToken(TokenTypes.IDENT).getText();
loggerSet.add(varName);
Comment thread
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)) {
Comment thread
mssfang marked this conversation as resolved.
Outdated
switch (methodNameStr) {
case FATAL: // log4j only
Comment thread
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) {
Comment thread
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ page at http://checkstyle.sourceforge.net/config.html -->
<module name="PackageName">
<property name="format" value="^(?=.{9,80}$)((com.microsoft.azure|com.azure)(\.[a-z]{1,20})*)+$"/>
</module>

<!-- Javadoc checks -->
<!-- See http://checkstyle.sf.net/config_javadoc.html -->
<module name="JavadocMethod">
Expand All @@ -266,5 +266,7 @@ page at http://checkstyle.sourceforge.net/config.html -->
2) They must implement a public static method named builder
-->
<module name="com.azure.tools.checkstyle.checks.ServiceClientChecks"/>
<module name="com.azure.tools.checkstyle.checks.GoodLoggingCheck"/>

</module>
</module>