8000 Update sonar-plugin-api to 10.0 and use slf4j Logger by alban-auzeill · Pull Request #300 · SonarSource/sonar-analyzer-commons · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Update sonar-plugin-api to 10.0 and use slf4j Logger #300

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
merged 3 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
<artifactId>sonar-plugin-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
Expand All @@ -42,6 +47,11 @@
<artifactId>sonar-plugin-api-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.api.plugin</groupId>
<artifactId>sonar-plugin-api-test-fixtures</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.SonarRuntime;
import org.sonar.api.server.rule.Context;
import org.sonar.api.server.rule.RuleDescriptionSection;
import org.sonar.api.server.rule.RuleDescriptionSectionBuilder;
import org.sonar.api.server.rule.RulesDefinition.NewRule;
import org.sonar.api.utils.Version;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.HOW_TO_FIX_SECTION_KEY;
import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.INTRODUCTION_SECTION_KEY;
Expand All @@ -44,7 +44,7 @@
*/
class EducationRuleLoader {

private static final Logger LOG = Loggers.get(EducationRuleLoader.class);
private static final Logger LOG = LoggerFactory.getLogger(EducationRuleLoader.class);
private static final String CODE_EXAMPLES_HEADER = "<h3>Code examples</h3>";
private static final String WHY_SECTION_HEADER = "<h2>Why is this an issue\\?</h2>";
private static final String HOW_TO_FIX_SECTION_HEADER = "<h2>How to fix it</h2>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.utils.Version;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

public class ExternalReportProvider {

private ExternalReportProvider() {
}

private static final Logger LOG = Loggers.get(ExternalReportProvider.class);
private static final Logger LOG = LoggerFactory.getLogger(ExternalReportProvider.class);

public static List<File> getReportFiles(SensorContext context, String externalReportsProperty) {
boolean externalIssuesSupported = context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(7, 2));
boolean externalIssuesSupported = context.runtime().getApiVersion().isGreaterThanOrEqual(Version.create(7, 2));
String[] reportPaths = context.config().getStringArray(externalReportsProperty);

if (reportPaths.length == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.StreamSupport;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProgressReport implements Runnable {

Expand Down Expand Up @@ -58,7 +58,7 @@ public ProgressReport(String threadName, long period, Logger logger, String adje
}

public ProgressReport(String threadName, long period, String adjective) {
this(threadName, period, Loggers.get(ProgressReport.class), adjective);
this(threadName, period, LoggerFactory.getLogger(ProgressReport.class), adjective);
}

public ProgressReport(String threadName, long period) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.slf4j.event.Level;
import org.sonar.api.SonarEdition;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarRuntime;
Expand All @@ -38,11 +39,11 @@
import org.sonar.api.server.rule.RulesDefinition.NewRepository;
import org.sonar.api.server.rule.RulesDefinition.Repository;
import org.sonar.api.server.rule.RulesDefinitionAnnotationLoader;
import org.sonar.api.testfixtures.log.LogTester;
import org.sonar.api.utils.Version;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonar.api.server.rule.RulesDefinition.Context;

public class EducationRuleLoaderTest {
Expand All @@ -52,9 +53,7 @@ public class EducationRuleLoaderTest {
private static final String RULE_REPOSITORY_KEY = "rule-definition-test";

@Rule
public LogTester logTester = new LogTester();
@Rule
public ExpectedException exceptionRule = ExpectedException.none();
public LogTester logTester = new LogTester().setLevel(Level.TRACE);

private Context context;
private NewRepository newRepository;
Expand Down Expand Up @@ -157,7 +156,7 @@ public void education_description_content_with_empty_sections() throws IOExcepti
newRepository.done();
RulesDefinition.Rule rule = context.repository(RULE_REPOSITORY_KEY).rule("MyRuleKey");

assertThat(String.join("\n", logTester.logs(LoggerLevel.DEBUG))).isEqualTo("Skipping section 'introduction' for rule 'MyRuleKey', content is empty\n" +
assertThat(String.join("\n", logTester.logs(Level.DEBUG))).isEqualTo("Skipping section 'introduction' for rule 'MyRuleKey', content is empty\n" +
"Skipping section 'resources' for rule 'MyRuleKey', content is empty");
assertThat(rule.ruleDescriptionSections()).hasSize(2);
assertThat(fallbackDescription).isEqualTo(testFileContent);
Expand Down Expand Up @@ -239,7 +238,7 @@ public void education_description_content_with_non_education_format() throws IOE
newRepository.done();
RulesDefinition.Rule rule = context.repository(RULE_REPOSITORY_KEY).rule("MyRuleKey");

assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty();
assertThat(logTester.logs(Level.ERROR)).isEmpty();
assertThat(rule.ruleDescriptionSections()).isEmpty();
assertThat(fallbackDescription).isEqualTo(testFileContent);
}
Expand All @@ -249,9 +248,9 @@ public void education_description_content_with_invalid_generic_and_specific_how_
EducationRuleLoader educationRuleLoader = new EducationRuleLoader(RUNTIME);
String testFileContent = getTestFileContent("invalid/S102.html");

exceptionRule.expect(IllegalStateException.class);
exceptionRule.expectMessage("Invalid education rule format for 'MyRuleKey', rule description has both generic and framework-specific 'How to fix it' sections");
educationRuleLoader.setEducationDescriptionFromHtml(newRule, testFileContent);
assertThatThrownBy(() -> educationRuleLoader.setEducationDescriptionFromHtml(newRule, testFileContent))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Invalid education rule format for 'MyRuleKey', rule description has both generic and framework-specific 'How to fix it' sections");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.event.Level;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.testfixtures.log.LogTester;

import static org.assertj.core.api.Assertions.assertThat;

public class ExternalReportProviderTest {

@Rule
public final LogTester logTester = new LogTester();
public final LogTester logTester = new LogTester().setLevel(Level.TRACE);
private final String EXTERNAL_REPORTS_PROPERTY = "sonar.foo.mylinter.reportPaths";

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import org.junit.Rule;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.LoggerLevel;
import org.slf4j.Logger;
import org.slf4j.event.Level;
import org.sonar.api.testfixtures.log.LogTester;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.atLeast;
Expand All @@ -39,7 +39,7 @@
public class ProgressReportTest {

@Rule
public LogTester logTester = new LogTester();
public LogTester logTester = new LogTester().setLevel(Level.TRACE);

@Test(timeout = 5000)
public void testSonarLogger() throws Exception {
Expand All @@ -48,7 +48,7 @@ public void testSonarLogger() throws Exception {
report.start(Arrays.asList("foo.java", "foo.java"));
report.stop();

assertThat(logTester.logs(LoggerLevel.INFO)).isNotEmpty();
assertThat(logTester.logs(Level.INFO)).isNotEmpty();
}

@Test(timeout = 5000)
Expand Down
10 changes: 10 additions & 0 deletions performance-measure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
<artifactId>sonar-plugin-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.api.plugin</groupId>
<artifactId>sonar-plugin-api-test-fixtures</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@

public abstract class Logger {

public static final String DEFAULT_SONAR_API_LOGGER = "org.sonar.api.utils.log.Loggers";
public static final String DEFAULT_SLF4J_LOGGER = "org.slf4j.LoggerFactory";

private static String sonarApiLogger = DEFAULT_SONAR_API_LOGGER ;
private static String loggerImpl = DEFAULT_SLF4J_LOGGER;

public static final Function<Class<?>, Logger> DEFAULT_FACTORY = cls -> {
try {
Class.forName(sonarApiLogger);
Class.forName(loggerImpl);
return new SonarSourceLoggerBridge(cls);
} catch (ClassNotFoundException e) {
// SonarSource logger not available
// slf4j logger not available
return new JavaLoggerBridge(cls);
}
};
Expand All @@ -55,7 +55,7 @@ public static Logger get(Class<?> cls) {
// Visible for testing
public static void overrideFactory(Function<Class<?>, Logger> factory, String sonarLoggerClass) {
Logger.factory = factory;
Logger.sonarApiLogger = sonarLoggerClass;
Logger.loggerImpl = sonarLoggerClass;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,31 @@
public class SonarSourceLoggerBridge extends Logger {

@SuppressWarnings("java:S1312")
private final org.sonar.api.utils.log.Logger delegate;
private final org.slf4j.Logger delegate;

public SonarSourceLoggerBridge(Class<?> cls) {
delegate = org.sonar.api.utils.log.Loggers.get(cls);
delegate = org.slf4j.LoggerFactory.getLogger(cls);
}

@Override
public void debug(Supplier<String> messageSupplier) {
delegate.debug(messageSupplier);
if (delegate.isDebugEnabled()) {
delegate.debug(messageSupplier.get());
}
}

@Override
public void info(Supplier<String> messageSupplier) {
delegate.info(messageSupplier.get());
if (delegate.isInfoEnabled()) {
delegate.info(messageSupplier.get());
}
}

@Override
public void warning(Supplier<String> messageSupplier) {
delegate.warn(messageSupplier.get());
if (delegate.isWarnEnabled()) {
delegate.warn(messageSupplier.get());
}
}

@Override
Expand Down
Loading
0