Skip to content

Commit

Permalink
SONARJAVA-4983 improve logging for partial analysis (#4932)
Browse files Browse the repository at this point in the history
  • Loading branch information
johann-beleites-sonarsource authored Nov 26, 2024
1 parent c9dd717 commit b097d2b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ private void registerCheckClasses(List<JavaCheck> destinationList, Checks<JavaCh
.sorted(Comparator.comparing(check -> classIndexes.getOrDefault(check.getClass(), Integer.MAX_VALUE)))
.toList();
destinationList.addAll(orderedChecks);
if (LOG.isDebugEnabled()) {
LOG.debug("Registered check: [{}]",
orderedChecks.stream()
.map(c -> c.getClass().getSimpleName() + " (" + createdChecks.ruleKey(c) + ")")
.collect(Collectors.joining(", ")));
}
jspChecks.addAll(orderedChecks.stream().filter(JspCodeVisitor.class::isInstance).toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,21 @@ public boolean scanWithoutParsing(InputFile inputFile) {
if (sonarComponents != null && sonarComponents.fileCanBeSkipped(inputFile)) {
PerformanceMeasure.Duration duration = PerformanceMeasure.start("ScanWithoutParsing");
boolean allScansSucceeded = true;

List<JavaFileScanner> scannersRequiringParsing = new ArrayList<>();
List<JavaFileScanner> scannersNotRequiringParsing = new ArrayList<>();

var fileScannerContext = createScannerContext(sonarComponents, inputFile, javaVersion, inAndroidContext, cacheContext);
for (var scanner: scannersThatCannotBeSkipped) {
boolean exceptionIsBlownUp = false;
PerformanceMeasure.Duration scannerDuration = PerformanceMeasure.start(scanner);
try {
allScansSucceeded &= scanner.scanWithoutParsing(fileScannerContext);
if (scanner.scanWithoutParsing(fileScannerContext)) {
scannersNotRequiringParsing.add(scanner);
} else {
scannersRequiringParsing.add(scanner);
allScansSucceeded = false;
}
} catch (AnalysisException e) {
// In the case where the IssuableSubscriptionVisitorsRunner throws an exception, the problem has already been
// logged and the exception formatted.
Expand All @@ -210,6 +219,10 @@ public boolean scanWithoutParsing(InputFile inputFile) {
}
}
duration.stop();

LOG.trace("Scanners that do not require parsing of {}: {}", inputFile, scannersNotRequiringParsing);
LOG.debug("Scanners that require parsing of {}: {}", inputFile, scannersRequiringParsing);

return allScansSucceeded;
} else {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,33 @@ void test_sonar_components() {
assertThat(sonarComponents.getJavaClasspath()).isEqualTo(list);
}

@Test
void verify_registration_logging_doesnt_trigger_on_info_level() {
logTester.setLevel(Level.INFO);

JavaCheck expectedCheck = new CustomCheck();
CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
SonarComponents sonarComponents = new SonarComponents(this.fileLinesContextFactory, null, null,
null, this.checkFactory, context.activeRules(), new CheckRegistrar[]{expectedRegistrar});
sonarComponents.setSensorContext(context);

assertThat(logTester.getLogs()).isEmpty();
}

@Test
void verify_registration_logging() {
logTester.setLevel(Level.DEBUG);

JavaCheck expectedCheck = new CustomCheck();
CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
SonarComponents sonarComponents = new SonarComponents(this.fileLinesContextFactory, null, null,
null, this.checkFactory, context.activeRules(), new CheckRegistrar[]{expectedRegistrar});
sonarComponents.setSensorContext(context);

assertThat(logTester.getLogs()).hasSize(2);
assertThat(logTester.getLogs().get(0).getRawMsg()).isEqualTo("Registered check: [{}]");
}

@Test
void creation_of_custom_checks() {
JavaCheck expectedCheck = new CustomCheck();
Expand Down

0 comments on commit b097d2b

Please sign in to comment.