Skip to content

Commit

Permalink
Hide log message about not loaded plugins because of disabled languag…
Browse files Browse the repository at this point in the history
…es (#167)
  • Loading branch information
remal committed Jul 15, 2023
1 parent e8b2a5d commit 8aa2cb6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ void beforeEach() {
void includedLanguage() {
project.getBuildFile().append("sonarLint.languages.include('java')");

project.assertBuildSuccessfully();
val buildLog = project.assertBuildSuccessfully().getOutput();
assertThat(buildLog)
.doesNotContainPattern("^Plugin .+ is excluded because language")
.doesNotContainPattern("^Plugin .+ is excluded because none of languages");

val issues = parseSonarLintIssuesOf("src/main/java/" + sourceFileRelativePath);
assertThat(issues)
Expand All @@ -193,7 +196,10 @@ void includedLanguage() {
void includedOtherLanguage() {
project.getBuildFile().append("sonarLint.languages.include('kotlin')");

project.assertBuildSuccessfully();
val buildLog = project.assertBuildSuccessfully().getOutput();
assertThat(buildLog)
.doesNotContainPattern("^Plugin .+ is excluded because language")
.doesNotContainPattern("^Plugin .+ is excluded because none of languages");

val issues = parseSonarLintIssuesOf("src/main/java/" + sourceFileRelativePath);
assertThat(issues)
Expand All @@ -205,7 +211,10 @@ void includedOtherLanguage() {
void excludedLanguage() {
project.getBuildFile().append("sonarLint.languages.exclude('java')");

project.assertBuildSuccessfully();
val buildLog = project.assertBuildSuccessfully().getOutput();
assertThat(buildLog)
.doesNotContainPattern("^Plugin .+ is excluded because language")
.doesNotContainPattern("^Plugin .+ is excluded because none of languages");

val issues = parseSonarLintIssuesOf("src/main/java/" + sourceFileRelativePath);
assertThat(issues)
Expand All @@ -217,7 +226,10 @@ void excludedLanguage() {
void excludedOtherLanguage() {
project.getBuildFile().append("sonarLint.languages.exclude('kotlin')");

project.assertBuildSuccessfully();
val buildLog = project.assertBuildSuccessfully().getOutput();
assertThat(buildLog)
.doesNotContainPattern("^Plugin .+ is excluded because language")
.doesNotContainPattern("^Plugin .+ is excluded because none of languages");

val issues = parseSonarLintIssuesOf("src/main/java/" + sourceFileRelativePath);
assertThat(issues)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,71 @@
package name.remal.gradle_plugins.sonarlint.internal.latest;

import static java.util.Collections.newSetFromMap;
import static java.util.Collections.unmodifiableMap;
import static org.sonarsource.sonarlint.core.commons.log.ClientLogOutput.Level.DEBUG;
import static org.sonarsource.sonarlint.core.commons.log.ClientLogOutput.Level.ERROR;
import static org.sonarsource.sonarlint.core.commons.log.ClientLogOutput.Level.TRACE;
import static org.sonarsource.sonarlint.core.commons.log.ClientLogOutput.Level.WARN;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import lombok.CustomLog;
import lombok.val;
import org.sonarsource.sonarlint.core.commons.log.ClientLogOutput;

@CustomLog
class GradleLogOutput implements ClientLogOutput {

private static final Set<String> LOGGED_MESSAGE = newSetFromMap(new ConcurrentHashMap<>());
private static final Map<Pattern, Level> MESSAGE_LEVELS;

static {
Map<Pattern, Level> messageLevels = new LinkedHashMap<>();

messageLevels.put(
Pattern.compile("No workDir in SonarLint"),
TRACE
);

messageLevels.put(
Pattern.compile(".+\\. Enable DEBUG mode to see them\\."),
TRACE
);

messageLevels.put(
Pattern.compile("Plugin '[^']+' is excluded because"
+ " (none of languages '[^']+' are|language '[^']+' is not) enabled"
+ "\\. Skip loading it\\."
),
DEBUG
);

messageLevels.put(
Pattern.compile("Plugin '[^']+' is excluded .+\\. Skip loading it\\."),
WARN
);

MESSAGE_LEVELS = unmodifiableMap(messageLevels);
}


private final Set<String> loggedMessages = newSetFromMap(new ConcurrentHashMap<>());

@Override
public void log(String formattedMessage, Level level) {
if (!LOGGED_MESSAGE.add(formattedMessage)) {
formattedMessage = formattedMessage.trim();

if (!loggedMessages.add(formattedMessage)) {
return;
}

if (formattedMessage.equals("No workDir in SonarLint")) {
level = DEBUG;
} else if (formattedMessage.startsWith("Plugin '") && formattedMessage.endsWith(". Skip loading it.")) {
level = WARN;
} else if (formattedMessage.endsWith(". Enable DEBUG mode to see them.")) {
level = TRACE;
for (val entry : MESSAGE_LEVELS.entrySet()) {
if (entry.getKey().matcher(formattedMessage).matches()) {
level = entry.getValue();
break;
}
}

if (level == ERROR) {
Expand Down

0 comments on commit 8aa2cb6

Please sign in to comment.